WF in IIS Hosted WCF Service

Hosting Workflow in WCF is described in the before mentioned article by Jeremy Boyd. In the article it is shown how to create an extension class for Workflow, and add this to the ServiceHost Extension collection. The code presented in the article is something like:

using (ServiceHost serviceHost = new ServiceHost(new ExpenseService()))

{

  WfWcfExtension wfWcfExtension = new WfWcfExtension("WorkflowRuntimeConfig");

  serviceHost.Extensions.Add(wfWcfExtension);

  serviceHost.Open();

  // block the process at this point, for example

  Console.ReadLine();

  serviceHost.Close();

}

While this works perfectly for a console server you may need to host the service in IIS at a later point in your development. As the example above show we need to add the extension to the ServiceHost at some point. The suggested way of doing this is to subclass the ServiceHost and add your service extensions in the OnOpening method. Example:

public class WFServiceHost : ServiceHost

{

  public WFServiceHost(Type t, params Uri[] baseAddresses) : base(t, baseAddresses){}

  public WFServiceHost(object singletonInstance, params Uri[] baseAddresses) :

    base(singletonInstance, baseAddresses) {}

  public WFServiceHost() : base(){}

  protected override void OnOpening()

  {

  WfWcfExtension wfWcfExtension = new WfWcfExtension("WorkflowRuntimeConfig");

  Extensions.Add(wfWcfExtension);

  base.OnOpening();

  }

}

IIS hosting of WCF Services is done adding a .svc file to your and in order to use your specialized servicehost you must provide a ServiceHostFactory that creates and instance of your servicehost:

public class WFServiceHostFactory : ServiceHostFactory

{

    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)

    {

        return new WFServiceHost(serviceType, baseAddresses);

    }

}

The .svc file should specify this as:

<%@ServiceHost language="c#" Factory="WorkflowServiceHost.WFServiceHostFactory" Service="SHP.ProvisioningService" %>