数据库准备:

1. 创建database(这里我们用的是MSSQL。Workflow支持其它数据库,但是MSSQL是配置最方便,不要问我为什么!)。

Workflow:添加工作流存储功能-LMLPHP

2. 运行位于[%WINDIR%\Microsoft.NET\Framework\v4.xxx\SQL\EN]的的脚本文件SqlWorkflowInstanceStoreSchema.sql和SqlWorkflowInstanceStoreLogic.sql。这时数据库中表应该类似于下图:

Workflow:添加工作流存储功能-LMLPHP

开工:

1. 在上一个项目的基础上,引入System.Activites.DurableInstancing和System.Runtime.DurableInstancing。如下图所示:

Workflow:添加工作流存储功能-LMLPHP

Workflow:添加工作流存储功能-LMLPHP

2. 修改Console Project的program.cs如下:

         static void Main(string[] args)
{
// Workflow Store of SQL Server
SqlWorkflowInstanceStore store =
new SqlWorkflowInstanceStore("Data Source=192.168.3.26;Initial Catalog=workflow_hour3;Persist Security Info=True;User ID=sa;Password=M@nager"); AutoResetEvent syncEvent = new AutoResetEvent(false); Activity wf = new WorkflowsProject.Activity1(); // Create the WorkflowApplication using the desired
// workflow definition.
WorkflowApplication wfApp = new WorkflowApplication(wf); // Assign workflow store to the current workflow
wfApp.InstanceStore = store; wfApp.PersistableIdle = delegate(WorkflowApplicationIdleEventArgs e)
{
Console.WriteLine("Workflow {0} persisted at {1}",
e.InstanceId, System.DateTime.Now.ToLongTimeString());
return PersistableIdleAction.Persist;
}; wfApp.Idle = delegate(WorkflowApplicationIdleEventArgs e)
{
Console.WriteLine("Workflow {0} idled at {1}",
e.InstanceId, System.DateTime.Now.ToLongTimeString());
syncEvent.Set();
}; // Handle the desired lifecycle events.
wfApp.Completed = delegate(WorkflowApplicationCompletedEventArgs e)
{
Console.WriteLine("Workflow {0} completed.", e.InstanceId.ToString());
syncEvent.Set();
}; wfApp.Aborted = delegate(WorkflowApplicationAbortedEventArgs e)
{
Console.WriteLine("Workflow {0} terminated because {1}", e.InstanceId, e.Reason.Message);
syncEvent.Set();
}; // Start the workflow.
wfApp.Run(); // Wait for Completed to arrive and signal that
// the workflow is complete.
syncEvent.WaitOne(); // Keep the console screen alive when workflow comnpletes
Console.WriteLine("Press enter to continue");
Console.Read();
}

3. 修改Delay的Duration为30秒,方便我们观察数据库的数据变化:

Workflow:添加工作流存储功能-LMLPHP

4. 运行。结果如下:

Workflow:添加工作流存储功能-LMLPHP

5. 在workflow persisted阶段,还没有到completed的时候,如果查看数据库中的[System.Activities.DurableInstancing].[InstancesTable]表,我们就会发现诸如如下的记录:

Workflow:添加工作流存储功能-LMLPHP

而当workflow运行到completed的时候,在查询这张表,就会发现这条记录已经不存在了。说明工作流的数据存储运行正确。

05-23 08:18