How to Host a WCF Service in IIS?

If you are new to WCF services then please read my earlier blogs. Go through with this link – WCF Tutorial step by step.WCF supports a 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 is 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 the 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 web site and add an 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 it’s time to consume the WCF service.
Step 13: Add a new Website in the 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.

Please follow and like us:

Leave a Comment