开发者

Trying to get Persistence Participant Promotion to work with a xamlx service in WF4

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

  • Andrew Zhu's here http://xhinker.com/post/WF4Promote-persistence-Data.aspx
  • Microsoft Samples

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 开发者_JAVA技巧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;
    }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜