数据库准备:
1. 创建database(这里我们用的是MSSQL。Workflow支持其它数据库,但是MSSQL是配置最方便,不要问我为什么!)。
2. 运行位于[%WINDIR%\Microsoft.NET\Framework\v4.xxx\SQL\EN]的的脚本文件SqlWorkflowInstanceStoreSchema.sql和SqlWorkflowInstanceStoreLogic.sql。这时数据库中表应该类似于下图:
开工:
1. 在上一个项目的基础上,引入System.Activites.DurableInstancing和System.Runtime.DurableInstancing。如下图所示:
2. 修改Console Project的program.cs如下:
1 static void Main(string[] args) 2 { 3 // Workflow Store of SQL Server 4 SqlWorkflowInstanceStore store = 5 new SqlWorkflowInstanceStore("Data Source=192.168.3.26;Initial Catalog=workflow_hour3;Persist Security Info=True;User ID=sa;Password=M@nager"); 6 7 AutoResetEvent syncEvent = new AutoResetEvent(false); 8 9 Activity wf = new WorkflowsProject.Activity1(); 10 11 // Create the WorkflowApplication using the desired 12 // workflow definition. 13 WorkflowApplication wfApp = new WorkflowApplication(wf); 14 15 // Assign workflow store to the current workflow 16 wfApp.InstanceStore = store; 17 18 wfApp.PersistableIdle = delegate(WorkflowApplicationIdleEventArgs e) 19 { 20 Console.WriteLine("Workflow {0} persisted at {1}", 21 e.InstanceId, System.DateTime.Now.ToLongTimeString()); 22 return PersistableIdleAction.Persist; 23 }; 24 25 wfApp.Idle = delegate(WorkflowApplicationIdleEventArgs e) 26 { 27 Console.WriteLine("Workflow {0} idled at {1}", 28 e.InstanceId, System.DateTime.Now.ToLongTimeString()); 29 syncEvent.Set(); 30 }; 31 32 // Handle the desired lifecycle events. 33 wfApp.Completed = delegate(WorkflowApplicationCompletedEventArgs e) 34 { 35 Console.WriteLine("Workflow {0} completed.", e.InstanceId.ToString()); 36 syncEvent.Set(); 37 }; 38 39 wfApp.Aborted = delegate(WorkflowApplicationAbortedEventArgs e) 40 { 41 Console.WriteLine("Workflow {0} terminated because {1}", e.InstanceId, e.Reason.Message); 42 syncEvent.Set(); 43 }; 44 45 // Start the workflow. 46 wfApp.Run(); 47 48 // Wait for Completed to arrive and signal that 49 // the workflow is complete. 50 syncEvent.WaitOne(); 51 52 // Keep the console screen alive when workflow comnpletes 53 Console.WriteLine("Press enter to continue"); 54 Console.Read(); 55 }
3. 修改Delay的Duration为30秒,方便我们观察数据库的数据变化:
4. 运行。结果如下:
5. 在workflow persisted阶段,还没有到completed的时候,如果查看数据库中的[System.Activities.DurableInstancing].[InstancesTable]表,我们就会发现诸如如下的记录:
而当workflow运行到completed的时候,在查询这张表,就会发现这条记录已经不存在了。说明工作流的数据存储运行正确。
Workflow:添加工作流存储功能,布布扣,bubuko.com
原文:http://www.cnblogs.com/ilovewindy/p/3718726.html