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
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
-
Create the workflow Application object
WorkflowApplication app = new WorkflowApplication(new ApprovalFlow());
-
Create the InstanceStore object
InstanceStore store = new SqlWorkflowInstanceStore(@"Data Source=.;Initial Catalog=SqlWorkflowInstanceStore;Integrated Security=True");
-
Set the InstanceStore of the workflow application object.
app.InstanceStore = store;
-
Run the workflow
app.Run();
-
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
-
Create the workflow Application object
WorkflowApplication app = new WorkflowApplication(new ApprovalFlow());
-
Create the InstanceStore object
InstanceStore store = new SqlWorkflowInstanceStore(@"Data Source=.;Initial Catalog=SqlWorkflowInstanceStore;Integrated Security=True");
-
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.