Hosting WCF Service On IIS

Hello everyone !

Today, I will explain the process of hosting of WCF (Windows Communication Foundation) Service on IIS (Internet Information Services). In my first article, I explained about creating a simple WCF Service and hosting it on a console application.

For WCF, several types of hosting are available.

  • Self Hosting in console application
  • Windows Form application hosting
  • Windows Service hosting
  • WAS hosting
  • IIS hosting

Here, I will explain you about IIS hosting. We have already developed WCF Service for addition in the previous article. So now, we have to host it on IIS.

  1. Open “MathService” WCF Service project with administrator rights (Run as Administrator) and then, add new web site (MathService_IIS) to its solution, as shown below.

  2. Now, add reference of the WCF Service to IIS Host project.

  3. Make the following changes in ‘Service.svc’ file.

    Remove the ‘CodeBehind="~/App_Code/Service.cs" and also change the Service parameter value to ‘Service="MathService.MathService".

  4. Now, open the Web.config file and paste the below code (attached in zip file too).

    1. <?xml version="1.0"?>  
    2. <configuration>  
    3.   <appSettings>  
    4.     <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />  
    5.   </appSettings>  
    6.   <system.web>  
    7.     <compilation debug="false" targetFramework="4.5.1" />  
    8.     <httpRuntime targetFramework="4.5.1"/>  
    9.   </system.web>  
    10.   <system.serviceModel>  
    11.     <services>  
    12.       <service behaviorConfiguration="Default" name="MathService.MathService">  
    13.         <endpoint address="" binding="basicHttpBinding" contract="MathService.IMathService" />  
    14.         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />  
    15.         <host>  
    16.           <baseAddresses>  
    17.             <add baseAddress="http://localhost:7777/MathService" />  
    18.           </baseAddresses>  
    19.         </host>  
    20.       </service>  
    21.     </services>  
    22.     <behaviors>  
    23.       <endpointBehaviors>  
    24.         <behavior name="webBehavior">  
    25.           <webHttp />  
    26.         </behavior>  
    27.       </endpointBehaviors>  
    28.       <serviceBehaviors>  
    29.         <behavior name="Default">  
    30.           <serviceMetadata httpGetEnabled="true"/>  
    31.           <serviceDebug includeExceptionDetailInFaults="true"/>  
    32.         </behavior>  
    33.       </serviceBehaviors>  
    34.     </behaviors>  
    35.   </system.serviceModel>  
    36. </configuration>  
  5. Now, IIS host project is ready. So, we have to open IIS and host this project on it. So, for opening IIS, type ‘inetmgr’ on run window and hit Enter. It will open the IIS.

  6. If IIS does not open and gives any error, then you have to first enable IIS from Windows features. For this, follow the below steps.

    • Open Control Panel =>> Programs and Features =>> Turn Windows features ON or OFF =>> Internet Information Services.
      And enable the checkbox, as shown in the below image.

    • Now, click on OK button. It will take some time and prompt you to restart the system. Then, Restart the system and open IIS by typing ’inetmgr’ command.

  7. After opening the IIS window, go to Server name, expand it, and go to ‘site’.

  8. Now, right click on ‘Site’ and go to ‘Add Website’.

  9. Now, give site name, physical path, and port. then, click on OK button.

  10. It will add new website on IIS. Now, select this site and click on ‘Content View’.

  11. Now, right click on ‘Service.scv’ file and go to ‘Browse’. This will open Service URL ( http://localhost:7777/Service.svc ) on browser.

  12. Now, our Service is successfully hosted on IIS. You can use it by using this URL http://localhost:7777/Service.svc.

Similar Articles