尝试让持久性参与者促销与 WF4 中的 xamlx 服务配合使用

发布于 2024-10-31 17:34:48 字数 5403 浏览 2 评论 0原文

我已经实现了工作流持久性参与者升级,正如 Microsoft 网站上所示:http://msdn.microsoft.com/en-us/library/ee364726%28VS.100%29.aspx,虽然一切似乎一切正常。查询时没有看到数据保存到数据库?我认为我错过了一个步骤或者微软错过了一些东西。

我正在使用工作流应用程序 .xamlx 服务,并且我已覆盖 WorkflowServiceHost。这一切似乎都工作正常,所以我不确定问题出在哪里?

所以我的问题是有人有一个关于如何实现持久性参与者的真实工作示例吗?

我在这里尝试了几种不同的做法,

但我似乎无法让它发挥作用。

仅供参考 - 当我更改 xnamespaces 以匹配时,此代码有效。感谢 Maurice

WorkflowServiceHost 代码:

public class ServiceHostFactory :WorkflowServiceHostFactory
{
    private static readonly string m_connectionString =
            "Data Source=localhost;Initial Catalog=WorkflowInstanceStore;Integrated Security=True";

    protected override WorkflowServiceHost CreateWorkflowServiceHost(Activity activity, Uri[] baseAddresses)
    {

        return base.CreateWorkflowServiceHost(activity, baseAddresses);

    }

    protected override WorkflowServiceHost CreateWorkflowServiceHost(WorkflowService service, Uri[] baseAddresses)
    {

        WorkflowServiceHost host = base.CreateWorkflowServiceHost(service, baseAddresses);
        host.DurableInstancingOptions.InstanceStore = SetupInstanceStore();

        SqlWorkflowInstanceStoreBehavior sqlWorkflowInstanceStoreBehavior = new SqlWorkflowInstanceStoreBehavior(m_connectionString);
        XNamespace xNS = XNamespace.Get("http://contoso.com/DocumentStatus");
        List<XName> variantProperties = new List<XName>() 
        { 
           xNS.GetName("UserName"), 
           xNS.GetName("ApprovalStatus"), 
           xNS.GetName("DocumentId"), 
           xNS.GetName("LastModifiedTime") 
        };
        sqlWorkflowInstanceStoreBehavior.Promote("DocumentStatus", variantProperties, null);
        host.Description.Behaviors.Add(sqlWorkflowInstanceStoreBehavior);

        //Add persistence extension here:
        host.WorkflowExtensions.Add<DocumentStatusExtension>(()=>new DocumentStatusExtension());;
        host.Description.Behaviors.Add(new ServiceMetadataBehavior() { HttpGetEnabled = true });            

        // Handle the UnknownMessageReceived event.
        host.UnknownMessageReceived += delegate(object sender, UnknownMessageReceivedEventArgs e)
        {
            Console.WriteLine(string.Format(CultureInfo.InvariantCulture, "Unknow Message Recieved:{0}", e.Message));
        };


        return host;
    }

    private static SqlWorkflowInstanceStore SetupInstanceStore()
    {


        SqlWorkflowInstanceStore sqlWorkflowInstanceStore = new SqlWorkflowInstanceStore(m_connectionString)
        {
            InstanceCompletionAction = InstanceCompletionAction.DeleteAll,
            InstanceLockedExceptionAction = InstanceLockedExceptionAction.BasicRetry,
            HostLockRenewalPeriod = System.TimeSpan.Parse("00:00:05")
        };
        InstanceHandle handle = sqlWorkflowInstanceStore.CreateInstanceHandle();



        //InstanceHandle handle = sqlWorkflowInstanceStore.CreateInstanceHandle();
        //InstanceView view = sqlWorkflowInstanceStore.Execute(handle, new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(30));
        //handle.Free();
        //sqlWorkflowInstanceStore.DefaultInstanceOwner = view.InstanceOwner;

        return sqlWorkflowInstanceStore;
    }

DocumentStatusExtension 代码:

        public string DocumentId;
    public string ApprovalStatus;
    public string UserName;
    public DateTime LastUpdateTime;

    private XNamespace xNS = XNamespace.Get("http://contoso.com/");

    protected override void CollectValues(out IDictionary<XName, object> readWriteValues, out IDictionary<XName, object> writeOnlyValues)
    {
        readWriteValues = new Dictionary<XName, object>();
        readWriteValues.Add(xNS.GetName("UserName"), this.UserName);
        readWriteValues.Add(xNS.GetName("ApprovalStatus"), this.ApprovalStatus);
        readWriteValues.Add(xNS.GetName("DocumentId"), this.DocumentId);
        readWriteValues.Add(xNS.GetName("LastModifiedTime"), this.LastUpdateTime);

        writeOnlyValues = null;
    }

    protected override IDictionary<XName, object> MapValues(IDictionary<XName, object> readWriteValues, IDictionary<XName, object> writeOnlyValues)
    {
        return base.MapValues(readWriteValues, writeOnlyValues);
    }

UpdateExtension 代码:

public sealed class UpdateExtension : CodeActivity
{
    // Define an activity input argument of type string
    public InArgument<string> Text { get; set; }

    // If your activity returns a value, derive from CodeActivity<TResult>
    // and return the value from the Execute method.
    protected override void Execute(CodeActivityContext context)
    {
        // Obtain the runtime value of the Text input argument
        context.GetExtension<DocumentStatusExtension>().DocumentId = Guid.NewGuid().ToString();
        context.GetExtension<DocumentStatusExtension>().UserName = "John Smith";
        context.GetExtension<DocumentStatusExtension>().ApprovalStatus = "Approved";
        context.GetExtension<DocumentStatusExtension>().LastUpdateTime = DateTime.Now;
    }
}

I have implemented workflow persistence participant promotion, just as it shown here on Microsoft's website:http://msdn.microsoft.com/en-us/library/ee364726%28VS.100%29.aspx and while everything seems like everything works. I do not see the data saving to the database when I query? I think I am missing a step or microsoft missed something.

I am using a workflow application .xamlx service and I have overridden the WorkflowServiceHost. This all seems to be working fine, So I am not sure where the problem can be?

So my question here is does anyone have a real working example of how to implement a persistence participant?

I tried a few different takes on this

But I just cannot seem to get this to work.

Just FYI-This code works when I changed the xnamespaces to match. Thanks to Maurice

WorkflowServiceHost Code:

public class ServiceHostFactory :WorkflowServiceHostFactory
{
    private static readonly string m_connectionString =
            "Data Source=localhost;Initial Catalog=WorkflowInstanceStore;Integrated Security=True";

    protected override WorkflowServiceHost CreateWorkflowServiceHost(Activity activity, Uri[] baseAddresses)
    {

        return base.CreateWorkflowServiceHost(activity, baseAddresses);

    }

    protected override WorkflowServiceHost CreateWorkflowServiceHost(WorkflowService service, Uri[] baseAddresses)
    {

        WorkflowServiceHost host = base.CreateWorkflowServiceHost(service, baseAddresses);
        host.DurableInstancingOptions.InstanceStore = SetupInstanceStore();

        SqlWorkflowInstanceStoreBehavior sqlWorkflowInstanceStoreBehavior = new SqlWorkflowInstanceStoreBehavior(m_connectionString);
        XNamespace xNS = XNamespace.Get("http://contoso.com/DocumentStatus");
        List<XName> variantProperties = new List<XName>() 
        { 
           xNS.GetName("UserName"), 
           xNS.GetName("ApprovalStatus"), 
           xNS.GetName("DocumentId"), 
           xNS.GetName("LastModifiedTime") 
        };
        sqlWorkflowInstanceStoreBehavior.Promote("DocumentStatus", variantProperties, null);
        host.Description.Behaviors.Add(sqlWorkflowInstanceStoreBehavior);

        //Add persistence extension here:
        host.WorkflowExtensions.Add<DocumentStatusExtension>(()=>new DocumentStatusExtension());;
        host.Description.Behaviors.Add(new ServiceMetadataBehavior() { HttpGetEnabled = true });            

        // Handle the UnknownMessageReceived event.
        host.UnknownMessageReceived += delegate(object sender, UnknownMessageReceivedEventArgs e)
        {
            Console.WriteLine(string.Format(CultureInfo.InvariantCulture, "Unknow Message Recieved:{0}", e.Message));
        };


        return host;
    }

    private static SqlWorkflowInstanceStore SetupInstanceStore()
    {


        SqlWorkflowInstanceStore sqlWorkflowInstanceStore = new SqlWorkflowInstanceStore(m_connectionString)
        {
            InstanceCompletionAction = InstanceCompletionAction.DeleteAll,
            InstanceLockedExceptionAction = InstanceLockedExceptionAction.BasicRetry,
            HostLockRenewalPeriod = System.TimeSpan.Parse("00:00:05")
        };
        InstanceHandle handle = sqlWorkflowInstanceStore.CreateInstanceHandle();



        //InstanceHandle handle = sqlWorkflowInstanceStore.CreateInstanceHandle();
        //InstanceView view = sqlWorkflowInstanceStore.Execute(handle, new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(30));
        //handle.Free();
        //sqlWorkflowInstanceStore.DefaultInstanceOwner = view.InstanceOwner;

        return sqlWorkflowInstanceStore;
    }

DocumentStatusExtension Code:

        public string DocumentId;
    public string ApprovalStatus;
    public string UserName;
    public DateTime LastUpdateTime;

    private XNamespace xNS = XNamespace.Get("http://contoso.com/");

    protected override void CollectValues(out IDictionary<XName, object> readWriteValues, out IDictionary<XName, object> writeOnlyValues)
    {
        readWriteValues = new Dictionary<XName, object>();
        readWriteValues.Add(xNS.GetName("UserName"), this.UserName);
        readWriteValues.Add(xNS.GetName("ApprovalStatus"), this.ApprovalStatus);
        readWriteValues.Add(xNS.GetName("DocumentId"), this.DocumentId);
        readWriteValues.Add(xNS.GetName("LastModifiedTime"), this.LastUpdateTime);

        writeOnlyValues = null;
    }

    protected override IDictionary<XName, object> MapValues(IDictionary<XName, object> readWriteValues, IDictionary<XName, object> writeOnlyValues)
    {
        return base.MapValues(readWriteValues, writeOnlyValues);
    }

UpdateExtension Code:

public sealed class UpdateExtension : CodeActivity
{
    // Define an activity input argument of type string
    public InArgument<string> Text { get; set; }

    // If your activity returns a value, derive from CodeActivity<TResult>
    // and return the value from the Execute method.
    protected override void Execute(CodeActivityContext context)
    {
        // Obtain the runtime value of the Text input argument
        context.GetExtension<DocumentStatusExtension>().DocumentId = Guid.NewGuid().ToString();
        context.GetExtension<DocumentStatusExtension>().UserName = "John Smith";
        context.GetExtension<DocumentStatusExtension>().ApprovalStatus = "Approved";
        context.GetExtension<DocumentStatusExtension>().LastUpdateTime = DateTime.Now;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不即不离 2024-11-07 17:34:48

我让它们工作,不幸的是我现在没有可以分享的示例代码。设置 PersistenceParticipant 时涉及的所有 XName 必须匹配可能有点棘手。此外,使用布尔值存在一个错误,它会在没有警告的情况下停止整个过程,因此请确保避免使用布尔值。

更新:
您在代码中使用不同的 XML 命名空间。 CreateWorkflowServiceHost() 使用 http://contoso.com/DocumentStatus 定义属性提升,而 CollectValues() 使用http://contoso.com/ 作为所收集值的 XML 命名空间。两者应该是相同的。

I have them working, unfortunately no sample code I can share just now. A PersistenceParticipant can be a bit tricky to setup with all the XNames involved that have to match up. Additionally there is a bug with using boolean values that stops the whole process without a warning so make sure you avoid booleans.

Update:
You are using different XML namespaces in your code. The CreateWorkflowServiceHost() uses http://contoso.com/DocumentStatus to define the property promotion and the CollectValues() uses http://contoso.com/ as the XML namespace of the values collected. Both should be the same.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文