Share via


Participating in Persistence

This sample demonstrates how a workflow extension can participate in persistence by storing data managed by the extension as part of the workflow instance state. The extension in the sample keeps track of the number of steps a workflow has taken. When the workflow is persisted, the extension state is persisted with it. When the workflow is loaded, the extension is initialized with the previously stored state. As a side note, this sample also demonstrates how activities can take responsibility for adding an extension to the workflow if the host has not done so.

Sample Details

The StepCountExtension is a simple extension with state tracking steps taken in a workflow. Steps are incremented by the IncrementStepCount activity. The current number of steps taken can be retrieved through the GetCurrentStepCount activity.

The StepCountExtension uses the PersistenceParticipant class to durably maintains its state. Prior to saving an instance, the CollectValues method is called on all extensions deriving from PersistenceParticipant. Values provided by the extension via the readWriteValues dictionary are stored with the instance state. Values are keyed by the XName property.

The PublishValues method is called on all extensions deriving from the PersistenceParticipant class after an instance has been loaded. The PublishValues method extracts the value added when the CollectValues method was called using the same XName.

When an instance completes and is deleted, any values associated with it in the readWriteValues dictionary are also deleted.

This sample unloads the instance each time it goes idle. The current step count is persisted, and when the instance is loaded and the StepCountExtension extension is added to the loaded WorkflowApplication, the step count is restored through the PublishValues method.

To use this sample

Note

This sample requires SQL Server Express, which is installed with Visual Studio 2010.

  1. Create the instance store database.

    1. Open a Visual Studio 2010 command prompt.

    2. Navigate to the sample directory (\WF\Basic\Persistence\PersistenceParticipants) and run CreateInstanceStore.cmd.

      Warning

      The CreateInstanceStore.cmd script attempts to create the database on the default instance of SQL Server 2008 Express. If you want to install the database on a different instance, modify the script to do so.

  2. Using Visual Studio 2010, open the PersistenceParticipants.sln solution file.

  3. To build the solution, press F6.

  4. To run the solution, press CTRL+F5.

    Warning

    If you installed the database on a non-default instance of SQL Server, update the connection string in the code prior to building the solution.

  5. Run the sample with administrator privileges by navigating to the project’s bin directory (\WF\Basic\Persistence\PersistenceParticipants\bin\Debug) in Windows Explorer, right-clicking Workflow.exe and selecting Run as Administrator.

  6. To remove the instance store database:

    1. Open a Visual Studio 2010 command prompt.

    2. Navigate to the sample directory (\WF\Basic\Persistence\PersistenceParticipants) and run RemoveInstanceStore.cmd.

Note

The samples may already be installed on your machine. Check for the following (default) directory before continuing.

<InstallDrive>:\WF_WCF_Samples

If this directory does not exist, go to Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4 to download all Windows Communication Foundation (WCF) and WF samples. This sample is located in the following directory.

<InstallDrive>:\WF_WCF_Samples\WF\Basic\Persistence\PersistenceParticipants

Build Date: 2010-02-04