How to load a persisted workflow from the database
Add project references to System.Activities.DurableInstancing and
System.Runtime.DurableInstancing. Open the Program.cs file and alter the
code to:
1 class Program { 2 static SqlWorkflowInstanceStore sqlWorkflowInstanceStore =SetupSqlPersistenceStore(); 3 static void Main(string[] args) { 4 StartAndUnloadInstance(); 5 } 6 static void StartAndUnloadInstance() { 7 AutoResetEvent waitHandler = new AutoResetEvent(false); 8 WorkflowApplication wfApp = new WorkflowApplication(new Workflow1()); 9 wfApp.InstanceStore = sqlWorkflowInstanceStore; 10 wfApp.PersistableIdle = (e) => { 11 return PersistableIdleAction.Unload; 12 }; 13 wfApp.Unloaded = (e) => { 14 waitHandler.Set(); 15 }; 16 Guid id = wfApp.Id; 17 wfApp.Run(); 18 waitHandler.WaitOne(); 19 LoadAndCompleteInstance(id); 20 } 21 static void LoadAndCompleteInstance(Guid id) { 22 Console.WriteLine("Press <enter> to load the persisted workflow"); 23 Console.ReadLine(); 24 AutoResetEvent waitHandler = new AutoResetEvent(false); 25 WorkflowApplication wfApp = new WorkflowApplication(new 26 Workflow1()); 27 wfApp.InstanceStore = sqlWorkflowInstanceStore; 28 wfApp.Unloaded = (workflowApplicationEventArgs) => { 29 waitHandler.Set(); 30 }; 31 wfApp.Load(id); 32 wfApp.Run(); 33 waitHandler.WaitOne(); 34 } 35 private static SqlWorkflowInstanceStore 36 SetupSqlPersistenceStore() { 37 string connectionString = 38 @"Data Source=.\sqlexpress;Initial Catalog=PersistenceDatabase; 39 Integrated Security=True"; 40 SqlWorkflowInstanceStore sqlWFInstanceStore = 41 new SqlWorkflowInstanceStore(connectionString); 42 sqlWFInstanceStore.InstanceCompletionAction = 43 InstanceCompletionAction.DeleteAll; 44 return sqlWFInstanceStore; 45 } 46 }
Line 31: wfApp.Load(id);