Friday, October 26, 2012

How to Configure SQL Persistence for Workflow 4?

Today's topic is how to enable workflow to persist data into SQL server. If you do not know what is windows workflow foundation, you can refer back my previous post here.

The reason we need to persist workflow into SQL server is because we cannot guarantee our web server is 24x7 online and problem free. So, we have the workflow instance and its data get persisted into a database, so that we do not need to worry about data loss when the server is offline. The workflow instance can be automatically restored after the server get online.

How to enable SQL persistence?

1. Create a new empty database call WorkflowInstanceStore.

2. Locate the following SQL scripts:
SqlWorkflowInstanceStoreSchema.sql and
SqlWorkflowInstanceStoreLogic.sql
from the folder C:\Windows\Microsoft.NET\Framework\v4.0.30319\SQL\en

3. Execute the SQL scripts in the correct order on the new empty database.
First, SqlWorkflowInstanceStoreSchema.sql
Second, SqlWorkflowInstanceStoreLogic.sql

Here is how the schema look like:



 
There are a few ways to host the workflow, but today's topic I only cover WAS-Hosted Workflow Services that setup the WorkflowServiceHost with configuration file. For Self-Hosted Workflow Service, you can refer to this MSDN article.

4. Open up your web host project web.config. Create a new service behavior for workflow instance store.

<behavior name="WorkflowServiceBehavior">
  <serviceMetadata httpGetEnabled="true"/>
  <serviceDebug includeExceptionDetailInFaults="true"/>
  <sqlWorkflowInstanceStore 
      connectionStringName="WorkflowPersistenceStore"
      hostLockRenewalPeriod="00:00:30"
      runnableInstancesDetectionPeriod="00:00:05"
      instanceCompletionAction="DeleteAll"
      instanceLockedExceptionAction="AggressiveRetry"
      instanceEncodingOption="GZip"/>
  <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>

5. Create a new connection string for workflow instance store.

<add name="WorkflowPersistenceStore" connectionString="server=.;database=WorkflowPersistenceStore;Trusted_Connection=true;multipleactiveresultsets=false" providerName="System.Data.SqlClient" />

Now your workflow application is enabled with SQL persistence. The following are the steps to create a sample project that work with workflow instance store.

Next, how to make use of workflow correlation token to continue the process flow from an active instance? Please read the next post.



No comments:

Post a Comment

Send Transactional SMS with API

This post cover how to send transactional SMS using the Alibaba Cloud Short Message Service API. Transactional SMS usually come with One Tim...