wf4.0支持mysql吗_WF4.0自定义持久化

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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值