Workflow Foundation 4.0 - Persistence

Ambily.raj
Posted by in WWF category on for Advance level | Points: 250 | Views : 21855 red flag

Workflow Foundation 4.0 is introduced a significant amount of changes from the previous versions of the technology. One of the main features of workflow is Persistence. In this article we will be discussing about Persistence.

Persistence

Persistence means storing the current state of a workflow. For better understanding, we will consider a document processing scenario.

In document processing, one user uploads the document and the workflow gets started. Then the document will go to the reviewer.  Reviewer will review the document, either accepts it or reject with comments. In this scenario, reviewing of the document can happen in a different application or can be done after many months.

When we deal with this kind of scenario, we will go for persistence. After uploading the document, the corresponding workflow state is stored in an InstanceStore. When the review starts, we can reload and continue execution of the workflow from the last executed state. In our scenario, the reviewer can use another application, which can reload the workflow from the InstanceStore and continue execution. Also, as we are storing the workflow to InstanceStore, we can manage the long running workflow effectively.

How to Setup InstanceStore

By default, workflow is providing support for database based InstanceStore. For setting a database as InstanceStore, run the following SQL scripts in your database 

  • SqlWorkflowInstanceStoreSchema.sql 
  • SqlWorkflowInstanceStoreLogic.sql

You can find these two scripts under C:\Windows\Microsoft.NET\Framework\v4.0.30319\SQL\en

We can create our own custom InstanceStore by inheriting from System.Runtime.DurableInstancing.InstanceStore class. We will discuss about the custom InstanceStore latter.

How to Persist a Workflow

The steps for Persisting a workflow to our InstanceStore is

  1. Create the workflow Application object 
    WorkflowApplication app = new WorkflowApplication(new ApprovalFlow());  
     
  2. Create the InstanceStore object 

    InstanceStore store = new SqlWorkflowInstanceStore(@"Data Source=.;Initial Catalog=SqlWorkflowInstanceStore;Integrated Security=True");

  3. Set the InstanceStore of the workflow application object. 

    app.InstanceStore = store;

  4. Run the workflow 

    app.Run();

  5. Persist the workflow 

    app.Persist();      

     

Note that, if you want to reload the workflow after sometime, we need to store the workflowId. 
      

Guid persistId = app.Id;

 
In our document processing scenario, we can save the workflow Id as part of the assigned person Id and when the user login, reload the workflow.
 
One more thing to note, Persist () will persists the current state of the workflow to the specified InstanceStore. It won’t reload your workflow from the memory. If you have some critical data or state transition, call the Persist to save the current state to InstanceStore and continue execution.

But, in our scenario, where the reviewer will login in a separate application or will login after one or two months, we can remove the workflow from memory. In this scenario, we need to call the Unload method instead of Persist. 
               

app.Unload();

 
Unload will persists the current state of the workflow to InstanceStore and unload the workflow object from memory too.

How to Reload the Workflow

For reloading a persisted workflow, we need to follow the steps

  1. Create the workflow Application object 

    WorkflowApplication app = new WorkflowApplication(new ApprovalFlow());   

  2. Create the InstanceStore object 

    InstanceStore store = new SqlWorkflowInstanceStore(@"Data Source=.;Initial Catalog=SqlWorkflowInstanceStore;Integrated Security=True");

  3. Set the InstanceStore of the workflow application object. 

    app.InstanceStore = store;

 Load the workflow 

app.Load(persistId);


 
Where, persistId is the workflow Id of the workflow persisted to the InstanceStore.

Conclusion

Here, we discussed about the Persistence, one of the main feature of workflow which implements the concept of separating the When and How of the logic. We can use persistence to run a workflow across multiple channels and across multiple users.

Page copy protected against web site content infringement by Copyscape

About the Author

Ambily.raj
Full Name: Ambily KK
Member Level: Silver
Member Status: Member,Microsoft_MVP,MVP
Member Since: 5/18/2010 1:05:25 AM
Country: India
Thanks Ambily K K http://ambilykk.com/
http://ambilykk.com/
I have over 9 years of experience working on Microsoft Technologies. I am carrying the passion on Microsoft technologies specifically on web technologies such as ASP .Net and Ajax. My interests also include Office Open XML, Azure, Visual Studio 2010. Technology adoption and learning is my key strength and technology sharing is my passion.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)