Using Persistence Services 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.|
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
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
Note: 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.
In the SQL Query Analyzer workspace, select the database that you created in step 1 in the list of available databases.
On the File menu, click Open, and open the SQL script %WINDIR%\Microsoft.NET\Framework\v3.0\Windows Workflow Foundation\SQL\<language>\SqlPersistenceService_Schema.
Execute the query by clicking Execute or by pressing F5 to create the SQL Persistence Service tables.
On the File menu, click Open, and open the SQL script %WINDIR%\Microsoft.NET\Framework\v3.0\Windows Workflow Foundation\SQL\<language>\SqlPersistenceService_Logic.
Execute the query by clicking Execute or by pressing F5 to create the SQL Persistence Service stored procedures.
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:
The workflow is created and started by the host.
Code1 displays the current UTC datetime to the console.
The workflow enters the delay1 activity.
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.
The Unload method calls into the SqlWorkflowPersistenceService service to persist the workflow instance. The workflow runtime then disposes of the workflow instance.
After at least five seconds, the timer expires.
The SqlWorkflowPersistenceService service loads the workflow from the database, and the runtime continues processing it.
Code2 then displays the UTC datetime to the console. The datetime should be five seconds later than the time displayed by code1.
When the workflow instance is completed, the workflow runtime calls into the SqlWorkflowPersistenceService service to persist the final state of the workflow.
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
Download the sample by clicking Download Sample.
This extracts the sample project to your local hard disk.
Click Start, point to Programs, point to Microsoft Windows SDK, and then click CMD Shell.
Go to the source directory of the sample.
At the command prompt, type MSBUILD <Solution file name>.
To run the sample
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.
Other ResourcesCustom Persistence Service Sample
Raise Event to Load Workflow Sample
Windows Workflow Persistence Services
Exercise 4 : Using Runtime Services
Using the SqlWorkflowPersistenceService Service
Task 3 : Using the Windows Workflow Persistence Service
© 2007 Microsoft Corporation. All rights reserved.