WCF Hosting in IIS Simplified

Microsoft introduced the Windows Communication Foundation (WCF) in .NET Framework v 3.0 with many exciting features, but what were the most exciting feature among them, or how is it different from ASMX services or ASP.NET Web Services? Or what additional things does it provide compared to ASMX services?

The primary difference is that an ASMX or ASP.NET web service is designed to send and receive messages using SOAP over HTTP only. While WCF can exchange messages using any format (SOAP is the default) over any transport protocol (HTTP, TCP/IP, MSMQ, NamedPipes and so on). Another WCF Tutorial has a detailed discussion of WCF Vs ASMX there.

So, if we are hosting a WCF Service in IIS (version 5 or 6), only the HTTP option is available as the transport protocol. In later versions of Internet Information Services (IIS), we can use any other protocol (TCP/IP, MSMQ, NamedPipes and so on.) as transport protocol.

As we already know that in order to host a WCF Service, we need a managed process. In case of Internet Information Services (IIS), it provides the host process and launch the process as the first request reaches the server.

Now let's first create a WCF service and later host it in IIS.

Create a StudentService Class Library

We have already created a StudentService Class Library project while implementing a WCF Self Hosting Tutorial. In order to make things consistent here, we will use the same service, so please go through Step 1 of that WCF Tutorial for creating a WCF Service.

WCF Hosting in IIS

So in order to host our WCF Service in IIS, use the following simple step-by-step approach.

  1. Add a website to our solution. Choose the “ASP.NET Empty Web Site” template. For web location, choose “HTTP” instead of “File System” and provide the path and press the “OK” button.

    Application

  2. Add a Reference of the StudentService project to our web site, in other words StudentIISHost.

    Add Reference

  3. Add a reference of System.ServiceModel to the web site.
  4. Add a new .svc file to our web site project as.

    svc file

    Reference

    In the code above, we are pointing the Service to StudentService in ServiceHost.
     
  5. The updated configuration for System.ServiceModel in the web.config will be as follows:
    1. <system.serviceModel>  
    2.       <behaviors>  
    3.            <serviceBehaviors>  
    4.                   <behavior name=”StudentServiceBehavior”>  
    5.                        <serviceMetadata httpGetEnabled=”true”/>  
    6.                        <serviceDebug includeExceptionDetailInFaults=”false”/>  
    7.                   </behavior>  
    8.            </serviceBehaviors>  
    9.       </behaviors>  
    10.       <services>  
    11.           <service behaviorConfiguration=”StudentServiceBehavior” name=”StudentService.StudentService”>  
    12.            <endpoint address=”http://localhost/StudentIISHost/MyStudentHost.svc”  
    13.                  binding=”wsHttpBinding”  
    14.                  contract=”StudentService.IStudentService”>  
    15.             <identity>  
    16.                 <dns value=”localhost”/>  
    17.             </identity>  
    18.           </endpoint>  
    19.           <endpoint address=”mex”  
    20.                 binding=”mexHttpBinding”  
    21.                 contract=”IMetadataExchange”/>  
    22.           </service>  
    23.        </services>  
    24.    </system.serviceModel> 
  6. Access the hosted WCF Service, in other words StudentService using the following path.

    http://localhost/StudentIISHost/MyStudentHost.svc

    WCF Service

Hopefully, this WCF Tutorial will help to understand the implementation for hosting a WCF Service in Internet Information Services (IIS).


Similar Articles