Partilhar via


Persisting a Workflow Application

This topic applies to Windows Workflow Foundation 4 (WF4).

This sample demonstrates how to run a WorkflowApplication, unload it when it goes idle, and then reload it to continue its execution.

Sample Details

WorkflowApplication is a host for a single workflow instance that provides a simple interface and enables several of the more common hosting scenarios. One such scenario is long running workflows facilitated by persistence. Host control of persistence is performed either by calling a persistence operation on the WorkflowApplication, or by handling a WorkflowApplication event and indicating that the WorkflowApplication should persist.

The sample workflow is a WriteLine activity prompting the user for their name, a ReadLine activity for receiving the name as input through the resumption of a Bookmark, and another WriteLine for echoing a greeting back to the user. When a workflow is waiting for input, this provides a natural point for persistence. This is often referred to as an Idle point. WorkflowApplication raises the Idle event whenever the workflow program can be persisted, is waiting on a bookmark resumption, and no other work is being performed. In this sample’s workflow, that point comes immediately after the ReadLine activity begins executing.

A WorkflowApplication is set up to perform persistence with an InstanceStore. This sample uses the SqlWorkflowInstanceStore. The InstanceStore must be assigned to the InstanceStore property before the WorkflowApplication is run.

The sample adds a handler to the PersistableIdle event. The handler for this event indicates what the WorkflowApplication should do by returning a PersistableIdleAction. When Unload is returned, the WorkflowApplication is unloaded.

The sample then accepts input from the user and loads the persisted workflow into a new WorkflowApplication. It does so by creating a new WorkflowApplication, recreating the InstanceStore, and associating the completed and unloaded events to the instance, and then calling Load with the identifier of the target workflow instance. Once the instance is acquired, the ReadLine activity’s bookmark is resumed. The workflow carries on execution from within the ReadLine activity and runs to completion. When the workflow completes and unloads, the InstanceStore is called one last time to delete the workflow.

To use this sample

  1. Open a Visual Studio 2010 command prompt.

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

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

    Dd807516.Caution(en-us,VS.100).gifCaution:
    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.

  3. Using Visual Studio 2010, open the Persistence.sln solution file and press CTRL+SHIFT+B to build it.

    Dd807516.Caution(en-us,VS.100).gifCaution:
    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.

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

To remove the instance store database

  1. Open a Visual Studio 2010 command prompt.

  2. Navigate to the sample directory and run RemoveInstanceStore.cmd.

Dd807516.Important(en-us,VS.100).gif Note:
The samples may already be installed on your computer. 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\InstancePersistence

See Also

Other Resources

AppFabric Hosting and Persistence Samples