[WF4]Create Your Own ServiceHostFactory

By using WorkflowServiceHost class, we can create a WF4 service host in code. The advantage of using WorkflowServiceHost is that we can add our own workflow extensions, tracking participant and persistence store.

So problem is: can use our own WorkflowServiceHost in IIS7 like we did in the console application? This blog entry is for answering this question.

1. Create a service factory named MyServiceHostFactory

 

using System;
using System.Collections.Generic;
using System.Workflow.Activities;
using System.Activities;
using System.Workflow.Runtime;
using System.ServiceModel.Activities.Activation;
using System.ServiceModel.Activities;
using System.ServiceModel.Description;
using System.Activities.DurableInstancing;
using System.Configuration;
 
public class MyServiceHostFactory : 
System.ServiceModel.Activities.Activation.WorkflowServiceHostFactory {     protected override WorkflowServiceHost
CreateWorkflowServiceHost(WorkflowService service,                                                   Uri[] baseAddresses) {         WorkflowServiceHost host = 
base
.CreateWorkflowServiceHost(service, baseAddresses);         string connectionString = 
ConfigurationManager
.AppSettings["SqlWF4PersistenceConnectionString"].ToString();         host.DurableInstancingOptions.InstanceStore =
new SqlWorkflowInstanceStore(connectionString);         return host;                  }     private SqlWorkflowInstanceStore SetupSimplySqlPersistenceStore() {         string connectionString =             ConfigurationManager.
AppSettings["SqlWF4PersistenceConnectionString"].ToString();         SqlWorkflowInstanceStore sqlInstanceStore =             new SqlWorkflowInstanceStore(connectionString);         sqlInstanceStore.HostLockRenewalPeriod = TimeSpan.FromSeconds(30);         return sqlInstanceStore;     } } 

 

2. Open web.config file and add a ServiceHostingEnvironment node under the System.serviceModel.

<serviceHostingEnvironment multipleSiteBindingsEnabled="true">
   <serviceActivations>
      <add relativeAddress="~/Service1.xamlx"
           service="Service1.xamlx"
           factory="MyXamlxFactory.MyServiceHostFactory"/>
   </serviceActivations>
</serviceHostingEnvironment>

 

Download source code:

 

CSWF4ServiceHostFactory.zip (14.39 kb)

blog comments powered by Disqus