Export (0) Print
Expand All

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 executed, the Windows Workflow Foundation can persist your workflow to storage until it has to 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.

NoteNote

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.

NoteNote

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.

NoteImportant

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.

NoteNote

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.

Creating the SQL Persistence Database

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 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. Run the query by clicking Execute, or by pressing 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. Run the query by clicking Execute, or by pressing F5 to create the SQL Persistence Service stored procedures.

Modifying the App.config File

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.

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

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

    NoteNote

    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, you can just 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();
    }
    
    
  8. 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);
    }
    
    
  9. 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);
    }
    
    
  10. Create a new static method named UnloadInstance that takes a Object named workflowInstance as a parameter.

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

  12. Build your project and run your application.

    Your output will appear similar to the following figure.

Compiling the Code

  1. Click Start, point to Programs, point to Microsoft .NET Framework SDK v2.0, and then click SDK Command Prompt.

  2. Go to the source directory of the tutorial.

  3. At the command prompt, type MSBUILD to build the project.

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

See Also

Footer image
Send comments about this topic to Microsoft.
Show:
© 2014 Microsoft