Sunday, August 01, 2010     | Register
 
How to use the SqlWorkflowPersistenceService

 

WF is designed to allow developers to create “reactive programs” i.e. programs that spend a lot of time waiting around for some external stimulus, burst into life for a brief period of execution and then go back to sleep again (like my pet cat).

 

Obviously, programs that behave in this way cannot be allowed to sit around in memory, blocking threads and hogging process while the wait for the necessary event (like dinner) to arrive. If we want to build robust and scalable WF applications the we need to provide the WF runtime with a persistence service. Once the persistence service is enabled, the WF runtime will happily save and resume your long running workflow instances with no further intervention required.

 

WF provides an “out of the box” persistence service that is designed for use with SQLServer. Other custom persistence services can be created if required. This article provides a step by step guide to enabling the SqlWorkflowPersistence service for your workflows.

 

Conceptually, there are three steps involved:
 
 wf persistence sequence


 
How To Create An Empty Database
 
You can create your database in SQLServer or SQLExpress using the tool of your choice (SQLServer management studio, Visual studio, command line, custom installer etc).
The screenshots below show the creation of a new SQLExpress database on a developers machine using Visual Studio 2008 (beta 2). You can call the database anything you want and make sure you have create permissions in the database (e.g. running as administrator if you are using Windows Vista!).
 
Step 1: Access the create new SQL server database context menu
 step 1

 

 

Step 2: Complete the new database dialog

 

 step 2
 
Step 3: A new empty database will be created

 

step 3

 


How to run the WF persistence SQL scripts
 

Windows workflow foundation provides two SQL scripts that allow you to create the structure for the SQL persistence database. The first thing you need to do is find the scripts! There are two script files:

 

·         SqlPersistenceService_Logic.sql
·         SqlPersistenceService_Schema.sql

 

They are normally installed to a directory path like C:\windows\Microsoft.NET\Framework\v3.0\Windows Workflow Foundation\SQL\EN .

If you are going to be doing a lot of WF development then it is probably a good idea to create a database project for the scripts and add them to your source control system. In the screenshot below I have a added the scripts to a visual studio 2005 database project. The scripts can be run by right clicking them (in the solution explorer or design surface) and clicking “run”!
Run the schema script first , then the logic script !
 

Step 4: Run the WF persistence SQL scripts

 step 4

 

 

 

How to tell the runtime to use the SqlWorkflowPersistenceService

 
The WF persistence service can be created and added in code or declaratively via the application configuration file.
 

To create the persistence service in code

 


We need to create the persistence service as a runtime service and add it to the WF runtime. To do this we simply create a new instance of the SqlWorkflowPersistenceService class, telling it where our database is etc.

The code can be as simple as the example code is show below:

 

String connectionString = "a connection string to your database!";

 

WorkflowRuntime runtime = new WorkflowRuntime();
SqlWorkflowPersistenceService persistenceService =
new SqlWorkflowPersistenceService(connectionString);
runtime.AddService(persistenceService);

 

 

To create the persistence service via the configuration file

 
For serious implementations, you will want your persistence service information to be derived from you applications configuration file. WF already supplies all the plumbing to read the configuration file and create the persistence service. All you have to do is add the appropriate entries.
To configure the WF runtime using the configuration file you need to first declare a configuration file section of type System.Workflow.Runtime.Configuration.WorkflowRuntimeSection. The section is then crated and the appropriate persistence service entries add. The same section can be used to add other service to the runtime if required.
A example configuration file for the SqlPersistenceService is shown below. The section is named “PersistenceRuntimeExample”.
 
<?xmlversion="1.0"encoding="utf-8" ?>
<configuration>
 
 <configSections>
    <section
     name="PersistenceRuntimeExample"
      type="System.Workflow.Runtime.Configuration.WorkflowRuntimeSection,
            System.Workflow.Runtime, Version=3.0.00000.0,
           Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
 </configSections>
 
 <PersistenceRuntimeExample>
   
    <CommonParameters>
      <addname="ConnectionString"value="Some connection string" />
    </CommonParameters>
   
    <Services>
      <add
        type="System.Workflow.Runtime.Hosting.SqlWorkflowPersistenceService,
              System.Workflow.Runtime, Version=3.0.00000.0, Culture=neutral,
              PublicKeyToken=31bf3856ad364e35"
              UnloadOnIdle="true"/>
    </Services>
 </PersistenceRuntimeExample>
 
</configuration>
 
The WF runtime is then created and configured automatically in code by passing the configuration section name in its constructor.
 
WorkflowRuntime runtime = new WorkflowRuntime("PersistenceRuntimeExample");

 

 

And thats all there is to it - enjoy!

 

 

Useful Resources

 

 Bart De Smet's Blog entry on WF persistence services .
Scott Allen’s hosting windows workflow article.
Print  


Copyright 2007 by Intelligent Decisioning Ltd   |  Privacy Statement  |  Terms Of Use