Partilhar via


Task 3: Use the Windows Workflow Persistence Service

Download sample

In this task you learn how to use the SqlWorkflowPersistenceService service in your application. When an assembly is idle for a while, which in this case occurs when the DelayActivity activity is run, the Windows Workflow Foundation can persist your workflow to storage until it must resume.

The SqlWorkflowPersistenceService service uses a SQL Server database to store the current state of your workflow through a process known as unloading. When the workflow is ready to resume, the SqlWorkflowPersistenceService service notifies the Windows Workflow Foundation runtime engine, and your workflow is subsequently loaded and execution resumes.

Note

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

Note

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

Note

You must have an App.config file for this task. If you did not follow the steps in Task 2: Configure Runtime Services using App.Config, you must create an App.config file before you continue.

Note

Although you are encouraged to follow the exercises in a linear manner, it is not required. You can start this exercise by opening the sample project and proceeding to the steps in the following section.

To create the SQL persistence database

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

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

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

  4. Click Execute to run the query or press F5 to create the SQL Persistence Service tables.

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

  6. Click Execute to run the query or press F5 to create the SQL Persistence Service stored procedures.

To modify App.config for the SqlWorkflowPersistenceService

  1. In the Services element in the App.config file, create a new element named add.

  2. Add an attribute named type to the add element that has a value of System.Workflow.Runtime.Hosting.SqlWorkflowPersistenceService, System.Workflow.Runtime, Version=3.0.00000.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.

  3. Add an attribute named connectionString to the add element that has a value of Initial Catalog=WorkflowPersistenceStore;Data Source=localhost;Integrated Security=SSPI;.

  4. Add an attribute named LoadIntervalSeconds to the add element that has the value 5.

    <add type="System.Workflow.Runtime.Hosting.SqlWorkflowPersistenceService, System.Workflow.Runtime, Version=3.0.00000.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionString="Initial Catalog=WorkflowPersistenceStore;Data Source=localhost;Integrated Security=SSPI;"
         LoadIntervalSeconds="5"/>
    

To create additional workflow event handlers

  1. In the Main method of the Program class, add a new event handler for the WorkflowLoaded event.

    workflowRuntime.WorkflowLoaded += new
        EventHandler<WorkflowEventArgs>(workflowRuntime_WorkflowLoaded);
    
  2. In the Main method of the Program class, add a new event handler for the WorkflowIdled event.

    workflowRuntime.WorkflowIdled += new
        EventHandler<WorkflowEventArgs>(workflowRuntime_WorkflowIdled);
    
  3. In the Main method of the Program class, add a new event handler for the WorkflowPersisted event.

    workflowRuntime.WorkflowPersisted += new
        EventHandler<WorkflowEventArgs>(workflowRuntime_WorkflowPersisted);
    
  4. In the Main method of the Program class, add a new event handler for the WorkflowUnloaded event.

    workflowRuntime.WorkflowUnloaded += new
       EventHandler<WorkflowEventArgs>(workflowRuntime_WorkflowUnloaded);
    
  5. Create a new static method named workflowRuntime_WorkflowLoaded.

    In the method body, use the WriteLine method to display an informative message.

    static void workflowRuntime_WorkflowLoaded(object sender, WorkflowEventArgs e)
    {
        Console.WriteLine("Workflow {0} loaded", e.WorkflowInstance.InstanceId);
    }
    
  6. Create a new static method named workflowRuntime_WorkflowIdled.

  7. In the method body, use the WriteLine method to display an informative message.

  8. Call the Unload method defined in the WorkflowInstance object in the WorkflowEventArgs parameter.

    Note

    The method that is described here to unload a WorkflowInstance when the workflow becomes idled enables you to perform application-specific logic before the workflow is persisted. If you do not need this type of functionality, add an attribute to the SqlWorkflowPersistenceService element in your application's App.config file named UnloadOnIdle, and set that attribute to true.

    static void workflowRuntime_WorkflowIdled(object sender, WorkflowEventArgs e)
    {
        Console.WriteLine("Workflow {0} idled", e.WorkflowInstance.InstanceId);
        e.WorkflowInstance.Unload();
    }
    
  9. Create a new static method named workflowRuntime_WorkflowPersisted.

    In the method body, use the WriteLine method to display an informative message.

    static void workflowRuntime_WorkflowPersisted(object sender, WorkflowEventArgs e)
    {
        Console.WriteLine("Workflow {0} persisted", e.WorkflowInstance.InstanceId);
    }
    
  10. Create a new static method named workflowRuntime_WorkflowUnloaded.

    In the method body, use the WriteLine method to display an informative message.

    static void workflowRuntime_WorkflowUnloaded(object sender, WorkflowEventArgs e)
    {
        Console.WriteLine("Workflow {0} unloaded", e.WorkflowInstance.InstanceId);
    }
    
  11. Create a new static method named UnloadInstance that takes a Object named workflowInstance as a parameter.

  12. In the UnloadInstance method, cast the workflowInstance parameter to a WorkflowInstance object, and call the Unload method to persist the workflow.

    static void UnloadInstance(object sender, WorkflowEventArgs e)
    {
        Console.WriteLine("Workflow {0} unloaded", e.WorkflowInstance.InstanceId);
        e.WorkflowInstance.Unload();
    }
    
  13. Build your project and run your application.

    Your output appears similar to the following figure.

    Output after finishing Task 3

Compiling the Code

For information about compiling your code, see Compiling the Code.

In Task 4: Use the Windows Workflow Tracking Service, you learn how to use SqlTrackingService to track workflow instance and activity events that occur during the running of your workflow.

See Also

Reference

SqlWorkflowPersistenceService

Concepts

Creating Custom Persistence Services

Other Resources

Task 4: Use the Windows Workflow Tracking Service
Using Persistence Services
Custom Persistence Service

Copyright © 2007 by Microsoft Corporation. All rights reserved.
Last Published: 2010-03-04