IIS Hosting of WCF Service

First let's create a simple WCF Service.

Step 1

Open Visual Studio and select "File" -> "New" -> "Project...". The following screen will open.



Select WCF Service Library and provide the name of the service as CarService. You can use whatever name for the service as you wish.

You will be able to see the following screen.



Step 2

Delete the Iservice1.cs and Service1.cs files from the project.



Step 3

Then add the two files IProduct.cs (interface file) and ProductService.cs to the project.



Step 4

In IProduct.cs (the interface file) write the following code.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.ServiceModel;  
  6.   
  7. namespace NorthwindServices  
  8. {  
  9.     [ServiceContract]  
  10.     public interface IProducts  
  11.     {  
  12.         [OperationContract]  
  13.         string GetProductName();  
  14.   
  15.         [OperationContract]  
  16.          string GetProductQty();  
  17.   
  18.         [OperationContract]  
  19.          string GetCategoryName();  
  20.     }  

Step 5

In the ProductService.cs file call the Interface file and write the definition of the Interface methods.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace CarService  
  7. {  
  8.     public class ProductService:IProduct  
  9.     {  
  10.         public string GetCarName()  
  11.         {  
  12.             return "Ford Figo";  
  13.         }  
  14.   
  15.         public string GetCarColor()  
  16.         {  
  17.             return "Sea Gray";  
  18.         }  
  19.   
  20.         public string GetQuantity()  
  21.         {  
  22.             return "1";  
  23.         }  
  24.     }  

Step 6

Now make the following changes in the App.config file.

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3.   
  4.   <system.web>  
  5.     <compilation debug="true" />  
  6.   </system.web>  
  7.   <!-- When deploying the service library project, the content of the config file must be added to the host's   
  8.   app.config file. System.Configuration does not support config files for libraries. -->  
  9.   <system.serviceModel>  
  10.     <services>  
  11.       <service name="CarService.ProductService">  
  12.         <endpoint address="" binding="wsHttpBinding" contract="CarService.IProduct">  
  13.           <identity>  
  14.             <dns value="localhost" />  
  15.           </identity>  
  16.         </endpoint>  
  17.         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />  
  18.         <host>  
  19.           <baseAddresses>  
  20.             <add           baseAddress="http://localhost:8732/Design_Time_Addresses/CarService/Service1/"/>  
  21.           </baseAddresses>  
  22.         </host>  
  23.       </service>  
  24.     </services>  
  25.     <behaviors>  
  26.       <serviceBehaviors>  
  27.         <behavior>  
  28.           <!-- To avoid disclosing metadata information,   
  29.           set the value below to false and remove the metadata endpoint above before deployment -->  
  30.           <serviceMetadata httpGetEnabled="True"/>  
  31.           <!-- To receive exception details in faults for debugging purposes,   
  32.           set the value below to true.  Set to false before deployment   
  33.           to avoid disclosing exception information -->  
  34.           <serviceDebug includeExceptionDetailInFaults="False" />  
  35.         </behavior>  
  36.       </serviceBehaviors>  
  37.     </behaviors>  
  38.   </system.serviceModel>  
  39.   
  40. </configuration> 

As you can see we are using wsHttpBinding. The Service name is <service name="CarService.ProductService"> Contract is contract="CarService.IProduct">,
dns value="localhost" /> & baseaddress is baseAddress="http://localhost:8732/Design_Time_Addresses/CarService/Service1/"/>.

Step 7

Now run the project. If you see the following screen then your WCF Service is working perfectly.



You can invoke the methods and get the results to test the service.

Step 8

Now add the SVC file to the project so that we can use this file later to publish on IIS.

Right-click on the project then select Add -> New Item then select Text File.

Give the name of the file as CarServiceHost.svc



Step 9

Just make the following changes in the SVC file.

  1. <%@ ServiceHost Service="CarService.ProductService" %> 

Step 10

Now build your solution then publish the project.



Give a target location on your hard drive.



Step 11

Now for the important part to host this service on IIS.

Go to Run -> inetmgr.

The following screen will open.



Click on Sites then select Add website.



Give the name of the service as CarService and select Application Pool as ASP.NET v4.0 as in the following:



Give the physical path of the service to the location where you have placed the published SVC file.



Give the port as 7742.



Now just open the browser and for the URL provide "http://localhost:7742/CarServiceHost.svc" or "http://ipaddress:7742/CarServiceHost.svc".The following page will open:



And that's it. You have successfully created and hosted a WCF Service on IIS. You can consume the service in an application and use it.