Persisting a Workflow Application

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 System.Runtime.Persistence.InstanceStore. This sample uses the SqlWorkflowInstanceStore. The System.Runtime.Persistence.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 System.Runtime.Persistence.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 System.Runtime.Persistence.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.


    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.


    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 File 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.


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


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.


See Also

AppFabric Hosting and Persistence Samples