尝试使持久性参与者促销在WF4中使用xamlx服务

时间:2011-04-11 14:23:18

标签: workflow workflow-foundation-4

我已经实现了工作流持久性参与者促销,就像它在微软网站上显示的那样: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;
    }
}

1 个答案:

答案 0 :(得分:4)

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

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