我正在尝试为我的应用程序编写一个示例 PersistenceParticipant。
它没有将属性存储在 [System.Activities.DurableInstancing].[InstancePromotedProperties]
工作流程似乎没有给出任何错误,但也不会进行保存。
public class MyPersistanceParticipant : PersistenceParticipant
{
public string MovieName;
public string Rating;
public Decimal Price;
static XNamespace xNS = XNamespace.Get("IamaUniqueNamespace");
//static XNamespace test = XNamespace.Get("clr-namespace:MovieRental.DataModel;assembly=MovieRental.DataModel");
//static XName currentCountName = stepCountNamespace.GetName("MovieName");
protected override void CollectValues(out IDictionary<System.Xml.Linq.XName, object> readWriteValues, out IDictionary<System.Xml.Linq.XName, object> writeOnlyValues)
{
//readWriteValues = new Dictionary<XName, object>();
//readWriteValues.Add(xNS.GetName("MovieName"), this.MovieName);
//readWriteValues.Add(xNS.GetName("Rating"), this.Rating);
//readWriteValues.Add(xNS.GetName("Price"), this.Price);
readWriteValues = null;
writeOnlyValues = new Dictionary<XName, object>();
writeOnlyValues.Add(xNS.GetName("MovieName"), this.MovieName);
writeOnlyValues.Add(xNS.GetName("Rating"), this.Rating);
writeOnlyValues.Add(xNS.GetName("Price"), this.Price);
//base.CollectValues(out readWriteValues, out writeOnlyValues);
}
protected override void PublishValues(IDictionary<XName, Object> readWriteValues)
{
object loadedData;
if (readWriteValues.TryGetValue(MovieName, out loadedData))
{
this.MovieName = (string)loadedData;
}
}
protected override IDictionary<XName, object> MapValues(IDictionary<XName, object> readWriteValues, IDictionary<XName, object> writeOnlyValues)
{
return base.MapValues(readWriteValues, writeOnlyValues);
}
}
我已使用以下代码将持久性存储添加到应用程序
MyPersistanceParticipant ps = new MyPersistanceParticipant();
_wfApp.Extensions.Add(ps);
我还添加了代码活动
public class MakePersistanceHappen : CodeActivity
{
protected override void Execute(CodeActivityContext context)
{
context.GetExtension<MyPersistanceParticipant>().MovieName = "Pursuit of Persistance";
context.GetExtension<MyPersistanceParticipant>().Rating = "VH";
context.GetExtension<MyPersistanceParticipant>().Price = Convert.ToDecimal(1);
}
}
当我调试 CollectValues 函数时,我可以看到代码运行正常,并且能够在即时窗口中看到这些值。但数据不会保存在商店中。
关于什么可能是错的任何指示?
我怀疑我是否正确使用了命名空间
静态 XNamespace xNS = XNamespace.Get("IamaUniqueNamespace");
我尝试了几个名称,其中一个来自 Workflow XAML 文件,但似乎都没有。有没有办法可以调试出了什么问题?我根本没有收到任何错误。
编辑 1:我能够解决我的问题我必须添加新的提升属性以确保它们保存在 [System.Activities.DurableInstancing].[InstancePromotedPropertiesTable] 并确保查询字符串正确。
//Add promoted properties
XNamespace dataNamespace = XNamespace.Get("IamaUniqueNamespace");
List<XName> variantProperties = new List<XName>();
variantProperties.Add(dataNamespace.GetName("MovieName"));
variantProperties.Add(dataNamespace.GetName("Rating"));
variantProperties.Add(dataNamespace.GetName("Price"));
_instanceStore.Promote("additionalProperty", variantProperties, null);
InstanceHandle handle = _instanceStore.CreateInstanceHandle();
InstanceView view = _instanceStore.Execute(handle,
new CreateWorkflowOwnerCommand(),
TimeSpan.FromSeconds(5));
handle.Free();
_instanceStore.DefaultInstanceOwner = view.InstanceOwner;
// Add instance stoe to app
wfApp.InstanceStore = _instanceStore;
// Add persistance participant as extension
MyPersistanceParticipant mp = new MyPersistanceParticipant();
wfApp.Extensions.Add(mp);
编辑 2:我提出这个问题的意图是让 PersistenceParticipant 在 [System.Activities.DurableInstancing].[InstancePromotedPropertiesTable] 中存储其他属性现在我想到了,我提出问题的方式可能不是很清楚。