WCF service has to be hosted in a Windows process called a host process. Single host process can host multiple servers and the same service type can be hosted in multiple host processes. A WCF service can be hosted by using several methods and IIS Hosting is one of them.

IIS Hosting stands for Internet Information Services. Its working model is similar to that of ASP.NET while hosting a WCF service. It uses the IIS features like recycling, idle shutdown, health monitoring, and message based activation.

Let us understand IIS Hosting with the below details.

  • Click File -> New -> Web site. Select “WCF Service” and Location as http. It will directly host the service in IIS.

  • Now, declare contracts in service interface.
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Runtime.Serialization;  
    5. using System.ServiceModel;  
    6. using System.ServiceModel.Web;  
    7. using System.Text;  
    8. // NOTE: You can use the "Rename" command on the "Refactor" menu to  
    9. // change the interface name "IService" in both code and config file  
    10. // together.  
    11. [ServiceContract]  
    12. Public interface IMyService {  
    13.     [OperationContract]  
    14.     String GetDetails(int value);  
    15.     [OperationContract]  
    16.     CompositeType GetDetailsUsingDataContract(CompositeType composite);  
    17.     // TODO: Add your service operations here  
    18. }  
    19. // Use a data contract as illustrated in the sample below to add  
    20. // composite types to service operations.  
    21. [DataContract]  
    22. Public class CompositeType {  
    23.     Bool boolValue = true;  
    24.     String stringValue = "Hello ";  
    25.     [DataMember]  
    26.     Public bool BoolValue {  
    27.         get {  
    28.             return boolValue;  
    29.         }  
    30.         set {  
    31.             boolValue = value;  
    32.         }  
    33.     }  
    34.     [DataMember]  
    35.     Public string StringValue {  
    36.         get {  
    37.             return stringValue;  
    38.         }  
    39.         set {  
    40.             stringValue = value;  
    41.         }  
    42.     }  
    43. }  
  • Now, implement the Service.
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Runtime.Serialization;  
    5. using System.ServiceModel;  
    6. using System.ServiceModel.Web;  
    7. using System.Text;  
    8. // NOTE: You can use the "Rename" command on the "Refactor" menu to  
    9. // change the class name "Service" in code, svc and config file  
    10. // together.  
    11. Public class MyService: IMyService {  
    12.     Public string GetDetail(int value) {  
    13.         Return string.Format("You entered: {0}", value);  
    14.     }  
    15.     Public CompositeType GetDetailUsingDataContract(CompositeType composite) {  
    16.         if (composite == null) {  
    17.             thrownewArgumentNullException("composite");  
    18.         }  
    19.         if (composite.BoolValue) {  
    20.             composite.StringValue += "Suffix";  
    21.         }  
    22.         return composite;  
    23.     }  
    24. }  
  • Now, Service file i.e. .svc will contain the name of the service and code behind file name. This file is used to know about the service.

    MyService.svc

    <%@ ServiceHost Language="C#" Debug="true" Service="MyService" CodeBehind="~/App_Code/MyService.cs" %>

  • Now, we need to configure Server-side configurations in Web.Config file. In this example we have configured only one end-point i.e 'wsHttpBinding'. However we can configure multiple endpoints with different bindings. Since we are going to host in IIS, we have to use only http binding.

    Web.Config
    1. <system.serviceModel>  
    2.     <services>  
    3.         <service behaviorConfiguration="ServiceBehavior" name="MyService">  
    4.             <endpoint address="http://localhost/IISHostedService/MyService.svc" binding="wsHttpBinding" contract="IMyService">  
    5.                 <identity>  
    6.                     <dns value="localhost" /> </identity>  
    7.             </endpoint>  
    8.             <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service>  
    9.     </services>  
    10.     <behaviors>  
    11.         <serviceBehaviors>  
    12.             <behavior name="ServiceBehavior">  
    13.                 <!-- To avoid disclosing metadata information,  
    14.   
    15. set the value below to false and remove the  
    16.   
    17. metadata endpoint above before deployment -->  
    18.                 <serviceMetadata httpGetEnabled="true" />  
    19.                 <!-- To receive exception details in faults for  
    20.   
    21. debugging purposes, set the value below to true.  
    22.   
    23. Set to false before deployment to avoid disclosing exception information -->  
    24.                 <serviceDebug includeExceptionDetailInFaults="false" /> </behavior>  
    25.         </serviceBehaviors>  
    26.     </behaviors>  
    27. </system.serviceModel>  
  • Now, you need to provide service file name and its address mentioned in config file.

    • Start -Run - inetmgr
    • Now, run the application