Pedro Félix's shared memory

WCF Web API–IIS Hosting

This is the fourth post on a series about the new WCF Web API – Preview 4.

In the second post, I described how to create self hosted services. In this post, I’ll show how to host on IIS.

1. Create a empty ASP.NET web application and map a IIS site or virtual directory into it. On my development environment, I usually create a new site for each project and use the hosts file to map the site name to 127.0.0.1.

2. Ensure the following on the web.config


  <!-- Ensure the UrlRoutingModule -->
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule"
               type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
  </system.webServer>

  <!-- Enable ASP.NET compatibility -->
  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
  </system.serviceModel>

The UrlRoutingModule is used to route the requests into the WCF runtime.

3. Add the following route map to the application start (global.asax)


public class Global : System.Web.HttpApplication
{
    protected void Application_Start(object sender, EventArgs e)
    {
        ...
        RouteTable.Routes.MapServiceRoute&lt;TodoResource&gt;("todos");
        ...
    }
}

where TodoResource is the resource class (service class) and “todos” is the base path.

The MapServiceRoute extension methods adds a ServiceRoute to the RouteTable.Routes collection. This ServiceRoute uses a host factory that will produce a HttpConfigurableServiceHost. This host derives from the new HttpServiceHost, introduced on a previous post.

The MapServiceRoute also accepts an IHttpHostConfigurationBuilder, which can be used to configure the service. This configuration model, also applicable to self-hosting scenarios, will be the subject of a future post.