Home > WF4

[WF4]Xml persistence store

4. August 2010

Many people want to store WF4 persistence data in xml file rather than in Sql Server(express). So I extract the xml persistence store code from the sample "PurchaseProcess".
XmlWorkflowInstanceStore.zip (4.72 kb)
Before using this xml persistence store, you need to set up the data folder in App.config file:

<configuration>

  <appSettings>

    <add key="WF4DataFolderDirectory" value="c:\"/>

  </appSettings>

 

Here is a using sample:
       Workflow1:     

    /// <summary>

    /// Start And Unload Instance

    /// </summary>

    static void StartAndUnloadInstance() {

        AutoResetEvent waitHandler = new AutoResetEvent(false);

        WorkflowApplication wfApp = new WorkflowApplication(new Workflow1());

        XmlWorkflowInstanceStore instanceStore =
                                  SetupXmlpersistenceStore(wfApp.Id);

        wfApp.InstanceStore = instanceStore;      

        ///persists application state and remove it from memory    
       
wfApp.PersistableIdle = (e) => {                      

            return PersistableIdleAction.Unload;

        };

        wfApp.Unloaded = (e) => {

            Console.WriteLine("unload");

            waitHandler.Set();

        };

        Guid id = wfApp.Id;

        wfApp.Run();

        waitHandler.WaitOne();

        LoadAndCompleteInstance(id, instanceStore);

    }

 

    static void LoadAndCompleteInstance(Guid id,InstanceStore instanceStore) {

        AutoResetEvent waitHandler = new AutoResetEvent(false);

        Console.WriteLine("Press <enter> to load the persisted workflow");

        Console.ReadLine();

        WorkflowApplication wfApp = new WorkflowApplication(new Workflow1());

        wfApp.InstanceStore = instanceStore;

        wfApp.Completed = (workflowApplicationCompletedEventArgs) => {

            Console.WriteLine("\nWorkflowApplication has Completed in the {0} state.",       workflowApplicationCompletedEventArgs.CompletionState);

        };

        wfApp.Unloaded = (workflowApplicationEventArgs) => {

            Console.WriteLine("WorkflowApplication has Unloaded\n");

            waitHandler.Set();

        };

        wfApp.Load(id);

        wfApp.Run();
        waitHandler.WaitOne();

    }

    private static XmlWorkflowInstanceStore SetupXmlpersistenceStore(Guid workflowId) {

        XmlWorkflowInstanceStore instanceStore = new XmlWorkflowInstanceStore(workflowId);

        InstanceHandle handle = instanceStore.CreateInstanceHandle();

        InstanceView view=instanceStore.Execute(handle,
                                                new CreateWorkflowOwnerCommand(),
                                                TimeSpan.FromSeconds(5));

        handle.Free();

        instanceStore.DefaultInstanceOwner=view.InstanceOwner;

        return instanceStore;

    }

WF4

Comments

Adi
Adi
3/1/2011 12:35:44 AM #
Hi,

Its a great post, i was exactly looking for this option, it creates the xml store but gives NullReferenceException was unhandled on wfApp.Load(id);

can you help me fixing the issue.

thanks
Adi
Adi
3/1/2011 12:40:05 AM #
istead of

WorkflowApplication wfApp = new WorkflowApplication(new Workflow1());

i am using

string path = @"C:\temp\WorkflowNewT.xaml";
FileStream xamlFile = new FileStream(path, FileMode.Open, FileAccess.Read);
Activity mathWF;
using (Stream mathXaml = File.OpenRead(path))
{
  mathWF = ActivityXamlServices.Load(mathXaml);
}

Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("ItemCode", "Glu");

StartAndUnloadInstance(mathWF);

Andrew
Andrew
4/14/2011 11:06:54 PM #
Would you mind send mend me a sample project? I will give it a test.
Francisco
Francisco
4/15/2011 10:25:49 AM #
How can you make this work with a WorkflowServiceHost?
Andrew
Andrew
4/15/2011 5:55:56 PM #
corrently, this xml store provider support only WorkflowApplication. I will look into to it and find a way to let it also run in WorkflowServiceHost. I will update here.
cakeMSDN
cakeMSDN
4/26/2011 3:26:41 AM #
How to write a Oracle InstanceStore,which Interface should I inherit,and which methods should I write?
12/10/2011 5:52:53 PM #
I like this article, thank you very much.
12/10/2011 5:54:14 PM #
The <a href="www.uk-christianlouboutin.net/"><strong>christian shoes</strong></a> is an insanely popular brand.
12/10/2011 5:57:57 PM #
I like this article, thank you very much.
12/10/2011 5:58:16 PM #
I like this article, thank you very much.
12/10/2011 6:36:32 PM #
I really like the fresh blog you did on the issue.
12/15/2011 2:43:50 AM #
Thanks for taking the time to discuss about this, I feel strongly about it and love learning more on this topic. If possible, would you mind updating your blog with more information? It is extremely helpful for me.
www.harveycp.com/family-christian-store.html
12/15/2011 2:45:31 AM #
Give me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! I’m sure you had fun writing this article.
12/15/2011 2:48:00 AM #
Give me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! I’m sure you had fun writing this article.
12/21/2011 12:45:01 AM #
Very useful many thanks, It looks like your subscribers might just want considerably more information such as this continue the great work.
12/28/2011 2:12:21 AM #
This has been very useful, thanks fro sharing this.
1/11/2012 3:50:09 AM #
I am very happy that I stumbled across this in my search for something relating to it.
1/16/2012 11:39:28 PM #
I found this is an informative and interesting post so i think so it is very useful and knowledgeable.

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading