Monday, March 11, 2013

Creating a Simple WCF in Visual Studio 2012

In this post I would like to explain on how to create a simple WCF using Visual Studio 2012 and deploy to the IIS server.

Step 01
Create a WCF Project. ( File > New > Project > WCF >WCF Service Application ). Select WCF Service Application and give a name.



                                                               
                                                                                                                                                   
Step 02
The Solution will include a WCF Service created by default. Let's delete this and create a new from scratch. You need to delete the files Service1.svc and IService1.cs.

Step 03
Create a new WCF service. Right click on the project, select "Add", then select New Item. In the Add New Item Window select "WCF Service" and provide a name for the service.


                                                                                                                                                                                                                                                                           
Step 04
Define the operation contract in the interface as the below example:
















                                                                                                                                                    
Step 05
Add the method and the logic in the service class and build the solution. (in SimpleService.svc.cs)
























                                                                    
Please note that I have used simple exception handling instead of the fault contract for simplicity.

Step 06 - Creating a site
Open the IIS Manager (In start menu select "Run" and type "inetmgr")
                                                     
Create an application pool















                                                                                                             
Create a site. You can specify the application pool to use.  

























                                                                                                                 
                                                                                   
Step 07 - Deploy Solution

In the solution explorer, right click on the project and select "Publish".



















                                                                                                                    

Click on the "Preview" button to check on the publishing actions to the files:




















                                                                                                         
Once the solution is published, you can check the service availability












  
                                                                                                        
Now the Service is ready for use.

Testing the Service

You can test the service functionality using the Visual Studio 2012 Developer command prompt. In the command prompt, type "wcftestclient".


                                                                                                                                                                                                   
In the WCF test client, right click on the "My Service Projects" and select "Add Service"
















                                                                                                                                                            
Insert the path for the service URL and select "OK".









                                                                                                             
The client opens up the service methods, double click on GetAverage(). Insert the values to the parameters and click "Invoke". You will get the resulting value.

No comments:

Post a Comment