Hosting a simple WCF service on IIS — a beginner and broad Overview

I have started exploring WCF. I am using VS2010 and .Net Framework 4.0. In this example below, I create a simple WCF service using the “VS2010 -> WCF Service Library” template. This creates a dll and the generated service at this point is very generic ie, it can be used with Http, TCP/IP or MSMQ binding. Now since it is a library, I can add many other WCF Services to this, at any point of time and all my services are in a single dll. Since the WCF service generated this way, does not have any *.svc file and is just a dll, I need a host application to invoke & launch my service. Because I want to host this service in IIS, I have 2 options. Either I can create a WCF Application using the template, “VS2010->File->New->Project->WCF Service Application”  OR  create a standard WCF Website using “VS2010->File->New->WebSite->WCFService” and run that in IIS. Also need to copy the serviceModel section of the concerned service from App.config to the related WCF application/WCF Website’s web.config.These configuration changes can be done using the in-built Configuration Editor too, by editing the Web.config file. The following example uses WCF Service Application to host the service from the library, in IIS.

Run VS2010->File->New->Project->WCF Service Library template.

Library1

In this Library , I added 2 WCF Service. Hence I have 2 interfaces — Service1 & Service2.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace BookLibrary1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetMyBook(string value);

        [OperationContract]
        string GetMyBookAuthor(string value);
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace BookLibrary1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService2" in both code and config file together.
    [ServiceContract]
    public interface IService2
    {
        [OperationContract]
        string DoWork();
    }
}

And here’s how the library’s app.config looks like
ServiceAppConfig2

Next create a WCF Service Application, which will implement both the services present in the library, I created already.

BookServiceApplication
Do a Add Reference to the dll generated earlier by the service Library.
You may or may not delete the IService1.cs or IService2.cs but those 2 .cs files are not needed at all.
Delete the .svc.cs file.
Open Service1.svc file. Remove the Codebehind attribute and change the Service attribute to refer to the desired service Method. As an example
<%@ ServiceHost Language=”C#” Debug=”true” Service=”BookLibrary1.Service1″ %>

Here’s how the WCF Service Application Solution looks like

TheServiceApplicationSolution

<%@ ServiceHost Language="C#" Debug="true" Service="BookLibrary1.Service2"  %>

Open Web.config. Replace system.serviceModel section from the library’s App.config file.

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>

  <!-- The serviceModel section has been copied from the librarys app.config -->
  <system.serviceModel>
    <services>
      <service name="BookLibrary1.Service1">
        <endpoint address="" binding="wsHttpBinding" contract="BookLibrary1.IService1">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8732/Design_Time_Addresses/BookLibrary1/Service1/" />
          </baseAddresses>
        </host>
      </service>
      <service name="BookLibrary1.Service2">
        <endpoint address="" binding="wsHttpBinding" contract="BookLibrary1.IService2">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8732/Design_Time_Addresses/BookLibrary1/Service2/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="True"/>
          <!-- 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 -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
  
  
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  
</configuration>

Build the app.
View the Service1.svc in browser.

Service1inBrowser
On a similar basis, I added another WCF Service from the library to my current application.

Now this WCF application is ready for IIS.

Few good links on WCF/Deployment which may help understand this better & in-depth.
http://msdn.microsoft.com/en-us/library/bb332338.aspx
http://keithelder.net/2009/09/02/video-wcf-deployment-strategies-on-channel9/

 Hope this helps. Thanks for reading.

Advertisements
This entry was posted in WCF. Bookmark the permalink.

2 Responses to Hosting a simple WCF service on IIS — a beginner and broad Overview

  1. Sunil says:

    There are lots of posts regarding this topic and i have gone through many of them but found this to be most straight forward and working in the first go.
    Thanks a ton

  2. Shay says:

    Hey! Someone in my Facebook group shared this site with us so I came to
    look it over. I’m definitely enjoying the information. I’m book-marking and will
    be tweeting this to my followers! Excellent blog and great design and style.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s