How to Host a WCF Service in IIS

If you are new to WCF services then please read my earlier blogs. Go thorugh with this link - WCF Tutorial step by step.

WCF supports various way to host a service. In this blog, I will explain about Hosting a WCF Service in IIS.

How to host WCF service in IIS?

Step 1:Create a WCF Library project in Visual studio.
Step 2: Delete Class and Interface which are created by default.
Step 3: Create an interface IProduct. Add one operation contract in this.
    interface IProduct
        string GetProduct(int productId);

Step 4: Add a class Product.cs to implement this interface.
class Product : IProduct
        public string GetProduct(int productId)
            return "Product Id - " + productId + "is available at store";

Step 5: Right click on Solution in Visual Studio. Add a new website in same solution. Select WCF Service.

Step 6: Delete files from App_Code. Add the reference of the WCF service project.

Step 7: Open .SVC file and remove code behind. .SVC file should look like this.
<%@ ServiceHost Language="C#" Service="WcfServiceLibraryMaster.Product" %>

Step 8: Now host your WCF service in IIS. Open IIS. Right click on Default website and add application. You may also create an independent site in IIS.

Step 9: Fill all the details.

Step 10: Once done. Right click on newly added Application or Website and select Switch to content view. You can see your service file there.

Step 11: Browse your file and you will see an output like this.

Step 12: Now its time to consume the WCF service.
Step 13: Add a new Website in same solution. Create ASP.Net website.
Step 14: Add service reference in this website.(Browse the .svc file in IIS and copy the url, right click on Website and add Service Reference.) 

Step 15: Once Service Reference added. You write code to consume WCF service that you created.
WCFServiceReference is the name which I gave while adding WCF service reference.

         WCFServiceReference.ProductClient objClient = new WCFServiceReference.ProductClient();
       string ss = objClient.GetProduct(1001);

Step 16: Your output will.

1001 is the Product Id which was passed from the client.

Product Id - 1001 is available at store


Popular posts from this blog

CAML Query in SharePoint 2010

Calling REST APi from server side code - C#

All about SharePoint 2010 Content Type Hub

SharePoint Interview Questions and Answers

Calling ASP.Net WebMethod using jQuery AJAX