publicclassSqlServerWorkflowInstanceStore : InstanceStore
{publicGuid ownerInstanceID;publicSqlServerWorkflowInstanceStore() :this(Guid.NewGuid())
{
}publicSqlServerWorkflowInstanceStore(Guid id)
{
ownerInstanceID=id;
}//Synchronous version of the Begin/EndTryCommand functionsprotectedoverrideboolTryCommand(InstancePersistenceContext context, InstancePersistenceCommand command, TimeSpan timeout)
{returnEndTryCommand(BeginTryCommand(context, command, timeout,null,null));
}//The persistence engine will send a variety of commands to the configured InstanceStore,//such as CreateWorkflowOwnerCommand, SaveWorkflowCommand, and LoadWorkflowCommand.//This method is where we will handle those commandsprotectedoverrideIAsyncResult BeginTryCommand(InstancePersistenceContext context, InstancePersistenceCommand command, TimeSpan timeout, AsyncCallback callback,objectstate)
{
IDictionarydata=null;//The CreateWorkflowOwner command instructs the instance store to create a new instance owner bound to the instanace handleif(commandisCreateWorkflowOwnerCommand)
{
context.BindInstanceOwner(ownerInstanceID, Guid.NewGuid());
}//The SaveWorkflow command instructs the instance store to modify the instance bound to the instance handle or an instance keyelseif(commandisSaveWorkflowCommand)
{
SaveWorkflowCommand saveCommand=(SaveWorkflowCommand)command;
data=saveCommand.InstanceData;
Save(data);
}elseif(commandisLoadWorkflowCommand)
{try{
InstancesTable obj=InstancesTableBiz.GetInstancesTable(this.ownerInstanceID);
System.Text.UTF8Encoding utf8=newSystem.Text.UTF8Encoding();byte[] bs=utf8.GetBytes(obj.InstanceXML);
System.IO.MemoryStream memoryStream=newSystem.IO.MemoryStream(bs);
data=LoadInstanceDataFromFile(memoryStream);
context.LoadedInstance(InstanceState.Initialized, data,null,null,null);
}catch(Exception exception)
{thrownewPersistenceException(exception.Message);
}
}returnnewCompletedAsyncResult(true, callback, state);
}protectedoverrideboolEndTryCommand(IAsyncResult result)
{returnCompletedAsyncResult.End(result);
}IDictionaryLoadInstanceDataFromFile(Stream inputStream)
{
IDictionarydata=newDictionary();
NetDataContractSerializer s=newNetDataContractSerializer();
XmlReader rdr=XmlReader.Create(inputStream);
XmlDocument doc=newXmlDocument();
doc.Load(rdr);
XmlNodeList instances=doc.GetElementsByTagName("InstanceValue");foreach(XmlElement instanceElementininstances)
{
XmlElement keyElement=(XmlElement)instanceElement.SelectSingleNode("descendant::key");
System.Xml.Linq.XName key=(System.Xml.Linq.XName)DeserializeObject(s, keyElement);
XmlElement valueElement=(XmlElement)instanceElement.SelectSingleNode("descendant::value");objectvalue=DeserializeObject(s, valueElement);
InstanceValue instVal=newInstanceValue(value);
data.Add(key, instVal);
}returndata;
}objectDeserializeObject(NetDataContractSerializer serializer, XmlElement element)
{objectdeserializedObject=null;
MemoryStream stm=newMemoryStream();
XmlDictionaryWriter wtr=XmlDictionaryWriter.CreateTextWriter(stm);
element.WriteContentTo(wtr);
wtr.Flush();
stm.Position=0;
deserializedObject=serializer.Deserialize(stm);returndeserializedObject;
}voidSave(IDictionaryinstanceData)
{XmlDocument doc=newXmlDocument();
doc.LoadXml("");foreach(KeyValuePairvalPairininstanceData)
{
XmlElement newInstance=doc.CreateElement("InstanceValue");
XmlElement newKey=SerializeObject("key", valPair.Key, doc);
newInstance.AppendChild(newKey);
XmlElement newValue=SerializeObject("value", valPair.Value.Value, doc);
newInstance.AppendChild(newValue);
doc.DocumentElement.AppendChild(newInstance);
}if(!string.IsNullOrEmpty(InstancesTableBiz.GetInstancesTable(this.ownerInstanceID).InstanceXML))
{
InstancesTable obj=InstancesTableBiz.GetInstancesTable(this.ownerInstanceID);
obj.InstanceXML=doc.InnerXml;
InstancesTableBiz.UpdateInstancesTable(obj);
}else{
InstancesTable obj=newInstancesTable();
obj.id=this.ownerInstanceID;
obj.InstanceXML=doc.InnerXml;
InstancesTableBiz.AddInstancesTable(obj);
}
}
XmlElement SerializeObject(stringelementName,objecto, XmlDocument doc)
{
NetDataContractSerializer s=newNetDataContractSerializer();
XmlElement newElement=doc.CreateElement(elementName);
MemoryStream stm=newMemoryStream();
s.Serialize(stm, o);
stm.Position=0;
StreamReader rdr=newStreamReader(stm);
newElement.InnerXml=rdr.ReadToEnd();returnnewElement;
}
}