WCF Service Hosts On IIS

IIS hosting is much reliable to other types of hosting. When we host our service on IIS, it automatically takes all the features of IIS like process recycling, Idle shutdown, message based activation and many others. There is also one disadvantage of IIS hosting, it only supports HTTP protocol.

Create WCF Project

Open Visual Studio, Go to File Menu, Choose New and then click on Project. It will open the following window where you can choose your WCF service. You can create your WCF service using “WCF Service Library” or “WCF Service Application”. You need to choose WCF Service Application and provide the suitable name for the service as “WCFServiceIISHostingDemo” and click on OK.

WCF Service Application

It will take some time to create a new WCF Service and create a sample template as in the following. We are going to create fresh step by step WCF service. So, first delete these two components: “IService1.cs” and “Service.svc”.


After deleting both files, right click on Project and choose Add and then choose New Item. It will open a popup window where you can choose different types of file to be added. You need to select WCF Service and provide the suitable name for it and click OK.

New Item

It will add an Interface for the service and also a service file. The following will be the structure of the WCF service.

WCF service

Now make some changes in the code as in the following;


  1. namespace WCFServiceIISHostingDemo  
  2. {  
  3.     public class MathService: IMathService  
  4.     {  
  5.         public int AddTwoNumber(int firstNumber, int secondNumber)  
  6.         {  
  7.             return firstNumber + secondNumber;  
  8.         }  
  9.     }  
  10. }  
  1. using System.ServiceModel;  
  2. namespace WCFServiceIISHostingDemo  
  3. {  
  4.     // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IMathService" in both code and config file together.  
  5.     [ServiceContract]  
  6.     public interface IMathService  
  7.     {  
  8.         [OperationContract]  
  9.         int AddTwoNumber(int firstNumber, int secondNumber);  
  10.     }  
  11. }  
  1. <?xml version="1.0"?>  
  2. <configuration>  
  4.   <appSettings>  
  5.     <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />  
  6.   </appSettings>  
  7.   <system.web>  
  8.     <compilation debug="true" targetFramework="4.5" />  
  9.     <httpRuntime targetFramework="4.5"/>  
  10.   </system.web>  
  11.   <system.serviceModel>  
  12.     <services>  
  13.       <service name="WCFServiceIISHostingDemo.MathService">  
  14.         <endpoint address="http://localhost/WCFServiceIISHostingDemo/MathService.svc" binding="wsHttpBinding" contract="WCFServiceIISHostingDemo.IMathService">  
  15.           <identity>  
  16.             <dns value="localhost"/>  
  17.           </identity>  
  18.         </endpoint>  
  19.         <endpoint  
  20.        address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>  
  21.       </service>        
  22.     </services>  
  23.     <behaviors>  
  24.       <serviceBehaviors>  
  25.         <behavior>  
  26.           <!-- To avoid disclosing metadata information, set the values below to false before deployment -->  
  27.           <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>  
  28.           <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->  
  29.           <serviceDebug includeExceptionDetailInFaults="false"/>  
  30.         </behavior>  
  31.       </serviceBehaviors>  
  32.     </behaviors>  
  35.     <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />  
  36.   </system.serviceModel>  
  37.   <system.webServer>  
  38.     <modules runAllManagedModulesForAllRequests="true"/>  
  39.     <!--  
  40.         To browse web app root directory during debugging, set the value below to true.  
  41.         Set to false before deployment to avoid disclosing web app folder information.  
  42.       -->  
  43.     <directoryBrowse enabled="true"/>  
  44.   </system.webServer>  
  46. </configuration>  
Now, it’s time to run the project. So, press F5. It will open a WCF Test Client. Here you will find your method, you can just pass your parameter and click on execute. It will show you the output.

WCF Test Client

Publish Your Service

It is time to publish your service that can be hosted on IIS. So, to publish your service right click on the project and choose publish. It will open a window where you can choose profile for publishing. You need to choose Custom and provide the name of profile.

Publish Your Service

And from the Connection tab, you can choose File System and provide the hosting path in wwwroot.


And finally click on Publish. It will take some time and publish your WCF service inside the wwwroot.

So, open integer from search program and you will find your publish directory here.

Right Click on publish directory “WCFServiceIISHostingDemo” and choose Convert Application. Provide default pool for this.


You will take care that default pool should be 4.0

default pool

Now run again from browser using the = following url.



So, finally service is hosted on IIS. The WCF service is running fine.

Thanks for reading this article, hope you enjoyed it.


Similar Articles