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;
}
cdc2de29-147f-41ca-87b7-c73345ab6438|2|5.0
WF4