Using Persistence Services Sample

Download sample

This sample illustrates how to use the SqlWorkflowPersistenceService service to load and unload workflows.

Many business processes take long periods of time to finish (up to many months or even years). Holding the workflow in memory is not only impractical (because of memory limitations), but it also prevents scaling because an instance must be processed on a single server. Many of these long-running workflows are not actively executing flow or process logic and are effectively idle, waiting for input from users or other systems. By unloading an idle instance, the host application can save memory and enable scalability across processing servers. This sample demonstrates how to unload an idle instance with a simple workflow that waits for five seconds before finishing.


The SqlWorkflowPersistenceService can also be set to unload idle workflows automatically by setting the UnloadOnIdle parameter to true in the constructor.

Database Configuration

The SQL services that are installed by Windows Workflow Foundation use Microsoft SQL Server to store information. You can use Microsoft SQL Server 2005 Express, SQL Server 2000 or later versions, or SQL Server 2000 Desktop Engine (MSDE) for these tasks.

Windows Workflow Foundation Setup does not install the databases that are required by these services; however, Setup installs the SQL scripts for creating and configuring the databases for these services.


The Microsoft Distributed Transaction Coordinator (MSDTC) service must be enabled for the SqlWorkflowPersistenceService to function.

The following steps describe how to create and configure the databases that are used by services in this sample.

To create and configure the SQL database

  1. Using SQL Server 2005 Express, SQL Server 2000 or later versions, or SQL Server 2000 Desktop Engine (MSDE), create a new database named SqlPersistenceService by using the SQL query statement:

    CREATE DATABASE SqlPersistenceService


    When using both the SqlTrackingService and the SqlWorkflowPersistenceService in a workflow application, it is recommended to use a single database for both persistence and tracking.

  2. In the SQL Query Analyzer workspace, select the database that you created in step 1 in the list of available databases.

  3. On the File menu, click Open, and open the SQL script %WINDIR%\Microsoft.NET\Framework\v3.0\Windows Workflow Foundation\SQL\<language>\SqlPersistenceService_Schema.

  4. Execute the query by clicking Execute or by pressing F5 to create the SQL Persistence Service tables.

  5. On the File menu, click Open, and open the SQL script %WINDIR%\Microsoft.NET\Framework\v3.0\Windows Workflow Foundation\SQL\<language>\SqlPersistenceService_Logic.

  6. Execute the query by clicking Execute or by pressing F5 to create the SQL Persistence Service stored procedures.

Sample Walkthrough

The workflow is a sequential workflow that contains three activities in sequence: code1, delay1, and code2. The CodeActivity activities write the current UTC datetime to the console and the DelayActivity activity waits five seconds before it continues. The host application manages the overall loading and unloading of the workflow instance in and out of memory by providing handlers for various WorkflowRuntime events.

The execution is as follows:

  1. The workflow is created and started by the host.

  2. Code1 displays the current UTC datetime to the console.

  3. The workflow enters the delay1 activity.

  4. The workflow instance is now idle waiting for the timer to expire. When the workflow becomes idle, it causes the WorkflowRuntime to raise the WorkflowIdled event.

  5. The Unload method calls into the SqlWorkflowPersistenceService service to persist the workflow instance. The workflow runtime then disposes of the workflow instance.

  6. After at least five seconds, the timer expires.

  7. The SqlWorkflowPersistenceService service loads the workflow from the database, and the runtime continues processing it.

  8. Code2 then displays the UTC datetime to the console. The datetime should be five seconds later than the time displayed by code1.

  9. When the workflow instance is completed, the workflow runtime calls into the SqlWorkflowPersistenceService service to persist the final state of the workflow.

Custom Services

This sample uses the out-of-box services for state persistence; however, you can also create customized state persistence services. You might have to do this to use current database systems or to provide additional functionality. For more information about how to develop custom services, see the samples listed in this topic and the Windows Workflow Foundation documentation.

To build the sample

  1. Download the sample by clicking Download Sample.

    This extracts the sample project to your local hard disk.

  2. Click Start, point to Programs, point to Microsoft Windows SDK, and then click CMD Shell.

  3. Go to the source directory of the sample.

  4. At the command prompt, type MSBUILD <Solution file name>.

To run the sample

  1. In the SDK Command Prompt window, run the .exe file in the PersistenceServices\bin\debug folder (or the PersistenceServices\bin folder for the VB version of the sample), which is located below the main folder for the sample.

See Also



Other Resources

Custom Persistence Service Sample
Raise Event to Load Workflow Sample
Windows Workflow Persistence Services
Exercise 4 : Using Runtime Services
Using the SqlWorkflowPersistenceService Service
Hosting Samples
Task 3 : Using the Windows Workflow Persistence Service

© 2007 Microsoft Corporation. All rights reserved.