Search code examples
workflow-foundation-4

WF4 Persistance Small Examples


I want to use the supported WF4 persistence for my WF4 Host, but I haven't seen any good, simple, concise examples to date. I was hoping someone could come up with a sample, or knows of a site that has these types of examples.


Solution

  • Open your SQL server management studio and make the persistence database.

    For .Net 4.5 Use the SQL scripts at: %systemroot%\Microsoft.NET\Framework\v4.0.30319\SQL\en SqlWorkflowInstanceStoreSchema.sql and SqlWorkflowInstanceStoreLogic.sql in that order.

    See: http://blogs.msdn.com/b/carlos/archive/2013/01/10/workflow-foundation-sql-scripts.aspx

    Then Open Visual Studion and make a workflow and attach it to a WorkflowApplication

    _workflowApplication = new WorkflowApplication(new International(), inParams);
    

    where International is the name of a workflow ( a XAML one in this case)

    Then connect the persistence provider to the WorkflowApplication:

        InstanceStore persistanceStore = new 
    
    SqlWorkflowInstanceStore(_persistenceDataBaseConnection);
    
    _workflowApplication.InstanceStore = persistanceStore;
    

    Where _persistenceDataBaseConnection is the connection string to your SQL instence.

    Now, when you run the workflow (_workflowApplication.Run();) the persistence store is ready for use.

    It will persist the workflow when you tell it to (with a Persist activity) or when the workflow waits/delays (if you set the callback delegate: for example

    _workflowApplication.PersistableIdle = WorkflowApplicationPersistableIdle;

    Where WorkflowApplicationPersistableIdle is a method you write.

    You should also save the Workflow ID somewhere for use when loading a persisted workflow ( see below)

    WorkFlowID = _workflowApplication.Id.ToString();
    

    There are a number of other features but those are the basics.

    To load the workflow from the Persistence store:

    Make a workflowApplication of the same type:

        _workflowApplication = new WorkflowApplication(new International(), inParams);
    

    and then Load it using the ID of the persisted workflow

    _workflowApplication.Load(_workflowID);