CustomInstanceStore is an abstraction over InstanceStore that makes it easy to create persistence providers for Windows Workflow Foundation.
By default, the only way to use long-running workflows in Windows Workflow Foundation is to connect it to a SQL Server. For instance, in an email approval workflow, it will save the state of the workflow instance to SQL Server, then load it back when the approval bookmark is triggered in the workflow.
Want it to save the workflow state to an XML file? To a MySQL database? That is not so easy.
CustomInstanceStore solves this.
package-install CustomInstanceStore
To implement the abstract class CustomInstanceStoreBase
, just override the Save()
and Load()
methods. They data key is a combination of a Store ID and an Instance ID.
Here is an example implementation that simply saves the instance data to the filesystem:
public class XMLFileInstanceStore : CustomInstanceStore.CustomInstanceStoreBase
{
string _basePath;
/// <summary>
/// Create store that persists instance state to XML file on the filesystem with a path of:
/// {basePath}\{storeId}\{instance}
/// </summary>
public XMLFileInstanceStore(string basePath, Guid storeId)
: base(storeId)
{
this._basePath = basePath;
}
public override void Save(Guid instanceId, Guid storeId, XmlDocument doc)
{
var filename = getFilePath(instanceId, storeId);
var directoryName = new FileInfo(filename).Directory.FullName;
Directory.CreateDirectory(directoryName);
doc.Save(filename);
}
public override XmlDocument Load(Guid instanceId, Guid storeId)
{
var filename = getFilePath(instanceId, storeId);
var xmlDoc = new XmlDocument();
xmlDoc.Load(filename);
return xmlDoc;
}
private string getFilePath(Guid instanceId, Guid storeId)
{
return Path.Combine(_basePath, storeId.ToString(), instanceId.ToString() + ".xml");
}
}
Then just use your new custom InstanceStore class instead of SqlWorkflowInstanceStore
in your application.
var app = new WorkflowApplication(new MyActivityDefinition());
var storeId = new Guid("0bfcc3a5-3c77-421b-b575-73533563a1f3"); // May be the same for all workflows in your application
app.InstanceStore = new XMLFileInstanceStore(@"c:\MyApp\WorkflowData", storeId);
See this MSDN article for general information on using persistence with Workflow Foundation.
- XML
- MemoryCache (useful for testing)
It should work on both 4.0 and 4.5 versions of Workflow Foundation.
Check out the GraphDocs project. Please note that it requires Neo4j to be running locally.
Browse the open issues, fork the repo, and submit a pull request!