Hosting a WCF Serviced Workflow in IIS 7 and using the persistence service (Visual Studio 2008)

 

 

By Serge Luca

MVP Windows Workflow Foundation

In the previous post I showed you how to host a "WCF serviced" workflow and how to use a persistence  service in a console application.

Now, let’s move our workflow service to IIS7. Code can be downloaded here.

1.Download the code of the previous post.

2.In IIS7 Admin Manager, create a virtual directory that will point to the WFServiceLibrary folder, assign a port number (for instance 8082). Make sure the service dll is in the .bin folder just below the virtual root.

sshot-185

3.In the virtual root, add a .svc file (myservice.svc)  with the following code:

<%@ServiceHost language=c# Debug="true" Service="WFServiceLibrary.Workflow1" Factory="System.ServiceModel.Activation.WorkflowServiceHostFactory" %>

4.Add a web.config file and configure it like this:

<configuration>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="WFServiceLibrary.Workflow1" behaviorConfiguration="WFServiceLibrary.Workflow1Behavior">
        <host>
          <baseAddresses>
            <add baseAddress="/" />
          </baseAddresses>
        </host>
        <endpoint address=""
                  binding="wsHttpContextBinding"
                  contract="WFServiceLibrary.IWorkflow1">
          <!–
              Upon deployment, the following identity element should be removed or replaced to reflect the
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity
              automatically.
          –>
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WFServiceLibrary.Workflow1Behavior"  >
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
          <serviceCredentials>
            <windowsAuthentication
                allowAnonymousLogons="false"
                includeWindowsGroups="true" />
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

5.Now test the svc file in the browser to verify that you can grab the metadata (wsd):

you should get this:

 

image

 

6.Add a console application project (name it IISClient) in order to invoke the service:

7.In the IISClient project add a service reference to the WCF service:

image

Keep the default name and click on ok. A proxy will be generated.

8.Add a reference to the System.WorkflowService assembly in the IISclient project .

9.Implement the client-side code :

static void Main(string[] args)

{

ServiceReference1.

Workflow1Client proxy= new ServiceReference1.Workflow1Client("WSHttpContextBinding_IWorkflow1");

// much faster to provide the endpoint directly, I don’t know why
// new ServiceReference1.Workflow1Client(new WSHttpContextBinding(),
// new EndpointAddress("http://localhost:8082/myservice.svc&quot;));

Console.WriteLine(proxy.GetData(0));

}

 

10.Run the code

 image

Ok that works !

11.Let’s modify the web.config file to register the persistence service:

add workflowRuntime setting in the default behavior:

image

Here is the workflowRuntime setting:

<

workflowRuntime name="WorkflowServiceHostRuntime" validateOnCreate="true" enablePerformanceCounters="true">

<

services>

<

add type="System.Workflow.Runtime.Hosting.SqlWorkflowPersistenceService, System.Workflow.Runtime, Version=3.0.00000.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"connectionString="Data Source=localhost\sqlexpress;Initial Catalog=PersistenceDB2;Integrated Security=True;Pooling=False"LoadIntervalSeconds="1" UnLoadOnIdle= "true" />

</

services>

</

workflowRuntime>

Make sure the persistence database (PersistenceDB2 here has been created, see my previous post for more infos)

12 (optional)If you want to access the WorkflowRuntime by code even if your service is hosted in IIS (and by the way an .svc file), then your need to create your own WorkflowServiceHost factory by creating a class derived from System.ServiceModel.Activation.WorkflowServiceHostFactory and by overriding its CreateServiceHost method.

 

  • In the WFServiceLibrary project  add a new class called MyCustomServiceHost derived from WorkflowServiceHostFactory (in our case we will register the persistence service in the WorkflowRuntime) :

public class MyCustomServiceHost : WorkflowServiceHostFactory

{

public override ServiceHostBase CreateServiceHost(string constructorString,Uri[] baseAddresses){

WorkflowServiceHost serviceHost=
(
WorkflowServiceHost)base.CreateServiceHost(constructorString, baseAddresses);
WorkflowRuntime wRuntime =serviceHost.Description.Behaviors.Find<WorkflowRuntimeBehavior>().WorkflowRuntime;

string connectionString=@"Data Source=localhost\sqlexpress;Initial Catalog=PersistenceDB2;Integrated Security=True;Pooling=False";

wRuntime.AddService(

new SqlWorkflowPersistenceService(connectionString,true,new TimeSpan(1,0,0), new TimeSpan(0,0,1)));

return serviceHost;

}

}

  • Modify the .svc file to use our new factory class:

<%

@ServiceHost language=c# Debug="true" Service="WFServiceLibrary.Workflow1" Factory="WFServiceLibrary.MyCustomServiceHost" %>

  • Remove the Persistence service definition in the web.config file.

This hands-on training is the property of U2U and may not be organized in class or in group without the prior written permission of U2U. Should you wish to organize this hands-on training in your company or institution, please contact U2U first to enter into a licence agreement. Each trainer or teacher using this hands-on training should have a licence agreement. Please ask your trainer or U2U whether he or she has entered into a licence agreement with U2U.

The hyperlink "www.u2u.be/msdna" to this hands-on training may be placed on your website for free, on the condition that the name and the logo of U2U are clearly mentioned in the reference. Please send us a mail containing the link to the web page our reference is used on.

 

 

 

 

 

Advertisements

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