Hydrating and Dehydrating Workflows

You can load a persistence service into a workflow runtime engine to enable the workflow runtime engine to hydrate and dehydrate workflow instances.

Dehydration occurs when a workflow instance enters the idle state, such as when a workflow pauses at a DelayActivity activity or is awaiting a response from a Web service call. The workflow runtime engine uses the persistence service to save the state of the workflow instance and then it unloads the workflow instance from memory. The persistence service serializes the workflow into an Extensible Application Markup Language (XAML) file and then stores it in a database, either in the Microsoft SQL Server database software or another database management tool. Hydration occurs when a previously dehydrated workflow instance becomes available to run again. For example, a DelayActivity activity might have expired or a Web service call might have returned a response. The workflow runtime engine uses the persistence service to reload the workflow instance and then resumes the workflow instance from where it paused.

What Are Long-Running Persistent Workflows?

One of the considerations when you design any application is the resources that are required. WF systems are no different.

The problem with WF applications is that business cases may take months or even years to complete.

To hold a workflow active in memory on a computer for this amount of time would be impractical due to factors such as hardware maintenance, system downtime, and running costs.

To enable you to implement long-running workflows, WF enables you to store workflows while they are running.

The workflow persistence service enables the workflow runtime engine to automatically store an idle workflow in an SQL database or another custom storage system and then retrieve the workflow and resume workflow execution when it is needed.

To enable this functionality, you create an instance of the workflow persistence service and add it to the workflow runtime engine.

The process of storing the workflow is called dehydration. When the workflow runtime engine identifies an idle workflow, it can release all of the resources that the workflow holds, serialize the workflow to a XAML file, and then store it in the database.

The workflow runtime engine automatically identifies a workflow that is idle, for example, when it is paused by a delay activity or waiting for a long-running call to a service. The process of retrieving the workflow is called hydration, which is the opposite process to dehydration.

The workflow XAML file is retrieved from the database, the workflow is deserialized, and resources are reacquired if necessary.

The workflow runtime engine can then continue to execute the workflow from the point where it was paused.

The workflow runtime engine identifies when a dehydrated workflow is required and initiates the hydration process automatically

Creating and Configuring a Persistence Database for Workflows

The SqlWorkflowPersistenceService persistence service requires a specific set of database tables and stored procedures. You must add these tables and stored procedures to the database that the persistence service uses to store information about dehydrated workflows. You can either create a new database or use an existing database.

The following provides details and guidance about how to create and configure a persistence database.

Step 1: Create a database

To create a database forthe SqlWorkflowPersistenceService service, execute an SQL statement such as the following:

CREATE DATABASE MyWorkflowPersistenceStore

You can create the database in several ways:

  • Open a command window and run the sqlcmdcommand-line tool. sqlcmd.exe is located in the C:\\Program Files\\Microsoft SQL Server\\90\\Tools\\Binn folder.

  • Open the SQL Server Management Studio and create the database by using the interface that is provided .

  • Use the Server Explorer pane in Visual Studio if the SQL Server installation is configured to enable this.

 

Step 2: Configure the database

To configure the database, the WF framework provides the following two SQL scripts to create the tables and stored procedures that are required by the SqlWorkflowPersistenceService persistence service. You must run these scripts in the order that is shown here:

1. SqlPersistence_Schema.sql

This script creates the tables that are required by the persistence service.

2. SqlPersistence_Logic.sql

This script creates the stored procedures that are required by the persistence service.

The SQL scripts are located in the %WINDIR%\\Microsoft.NET\\Framework\\v3.0\\Windows Workflow Foundation\\SQL\\<language> folder

There are various ways to run the SQL scripts:

  • Open a command window and run the sqlcmd command-line tool. sqlcmd is located in the C:\\Program Files\\Microsoft SQL Server\\90\\Tools\\Binn folder.

  • Start SQL Server Management Studio, open the SQL scripts, and then run the scripts within the SQL Server Management Studio environment.

  • In Visual Studio, create a database project. Add the SQL scripts to the project and then run the scripts. The demonstration at the end of this lesson shows you how to do this

 

For more information about how to create and configure a database for the SqlWorkflowPersistenceService persistence service, see Using SqlWorkflowPersistenceService.

Adding the Persistence Service to the Workflow Runtime Engine

You must add the persistence service to the workflow runtime engine to enable the workflow runtime engine to hydrate and dehydrate workflows. You can add the persistence service declaratively in the application configuration file or programmatically by using code. The following table describes these two methods.

Method 1: Declaratively

To add a persistence service declaratively, in the <Services> element in the app.config file, create a new element that is named <add> and specify the following information:

  • Type information for the persistence service.

  • A connection string for the persistence database.

  • A Boolean flag that indicates whether the workflow runtime engine should unload workflows from memory when they are idle.

In the job aid for this topic, there is an app.config example that shows you how to add the SqlWorkflowPersistenceService persistence service.

 

Method 2: Programmatically

To add a persistence service programmatically, create an instance of the persistence service class and specify the following information:

  • A connection string for the persistence database.

  • A Boolean flag that indicates whether the workflow runtime engine should unload workflows from memory when they are idle.

  • The length of time that the workflow runtime engine will maintain locks on idle workflows.

  • The frequency at which the persistence service will poll the database for workflows that have expired timers.

After you create the persistence service object, add the service to the workflow runtime engine by invoking the AddService method on the workflow runtime engine object.

The following app.config example shows how to add the SqlWorkflowPersistenceService persistence service. The example assumes that the persistence database is located on the local instance of SQL Server Express Edition and that the database is named MyWorkflowPersistenceStore.

 

<Services>

 

 <add

  type="System.Workflow.Runtime.Hosting.SqlWorkflowPersistenceService, 

        System.Workflow.Runtime,

        Version=3.0.00000.0,

        Culture=neutral,

        PublicKeyToken=31bf3856ad364e35"

 

  connectionString="Data Source=.\\SQLEXPRESS;

                    Initial Catalog=MyWorkflowPersistenceStore;

                    Integrated Security=SSPI"

 

  UnloadOnIdle="true"

 />

 

</Services>

 

 

 

The following code examples show how to add the SqlWorkflowPersistenceService persistence service to the workflow runtime engine. The examples assume that the persistence database is located on the local instance of SQL Server Express Edition; this database is named MyWorkflowPersistenceStore. The examples specify that the engine should dehydrate idle workflows. The examples also specify the maximum allowable time span for locks on idle workflows and a frequency of two seconds for polling for expired timers.

 

[Visual Basic]

Using runtime As New WorkflowRuntime()

 

  Dim connectionString As String = _

    "Data Source=.\\SQLEXPRESS;" & _

    "Initial Catalog=MyWorkflowPersistenceStore;" & _

    "Integrated Security=SSPI"

 

  Dim persistenceService As New SqlWorkflowPersistenceService( _

                                      connectionString, _

                                      True, _

                                      TimeSpan.MaxValue, _

                                      New TimeSpan(0, 2, 0))

 

  workflowRuntime.AddService(persistenceService)

 

 

Tags: