Workflow Foundation - 持久性额外数据

时间:2016-05-10 14:29:47

标签: c# asp.net workflow workflow-foundation

我使用Workflow Foundation和ASP.NET创建了一个小应用程序。 我在FlowChart中有多个人机交互步骤。 对于涉及持久性的工作流的每个步骤(=在每个人工交互之前),我想保存/更新一个额外的参数,比如包含很少字符串属性的“worklow对象”。

我查看了存储的可扩展性解决方案(https://msdn.microsoft.com/en-us/library/ee364726.aspx),但我无法启动并运行:用于持久性的数据库的表“InstancePromotedPropertiesTable”未更新且完全是空。

MSDN上没有提及任何具体行动吗?

是否还有其他解决方案可以在工作流程中附加我们可以在简历期间或之前读取/写入的额外自定义数据?

根据MSDN上提供的文档,以下是我的代码示例:

创建工作流程应用程序:

        private WorkflowApplication CreateWorkflow<T>(IDictionary<string, object> dictionary) where T : Activity, new()
    {
        WorkflowApplication workflowApplication;

        if (dictionary.Any())
        {
            workflowApplication = new WorkflowApplication(new T(), dictionary);
        }
        else
        {
            workflowApplication = new WorkflowApplication(new T());
        }

        workflowApplication.InstanceStore = new SqlWorkflowInstanceStore(InstanceStoreConnectionString);

        workflowApplication.PersistableIdle = (e) =>
        {
            return PersistableIdleAction.Persist;
        };

        workflowApplication.Completed = (e) =>
        {
            _isCompleted = true;
            _instanceUnloaded.Set();
        };
        workflowApplication.Idle = (e) =>
        {
            _instanceUnloaded.Set();
        };

        workflowApplication.Unloaded = (e) =>
        {

        };

        DocumentStatusExtension documentStatusExtension = new DocumentStatusExtension();
        workflowApplication.Extensions.Add(documentStatusExtension);
        return workflowApplication;
    }

进入空闲状态之前的步骤的执行方法:

protected override void Execute(NativeActivityContext context)
    {
        var bookmarkName = context.GetValue(StepName);

        context.GetExtension<DocumentStatusExtension>().DocumentId = Guid.NewGuid().ToString();
        context.GetExtension<DocumentStatusExtension>().UserName = "John Smith";
        context.GetExtension<DocumentStatusExtension>().ApprovalStatus = "Approved";
        context.GetExtension<DocumentStatusExtension>().LastUpdateTime = DateTime.Now;

        // Create a bookmark allows the persistance in the DB
        context.CreateBookmark(bookmarkName, new BookmarkCallback(this.Continue));
    }

调用Promote方法的PersistenceParticipant类:

public class DocumentStatusExtension : PersistenceParticipant
{
    public string DocumentId;
    public string ApprovalStatus;
    public string UserName;
    public DateTime LastUpdateTime;

    protected override void CollectValues(out IDictionary<XName, object> readWriteValues, out IDictionary<XName, object> writeOnlyValues)
    {
        //All these properties are passed to SqlWorkflowInstanceStore by the persistence framework through the SaveWorkflowCommand.InstanceData collection.

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

        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;

        SqlWorkflowInstanceStore store = new SqlWorkflowInstanceStore(System.Configuration.ConfigurationManager.AppSettings["InstanceStoreConnectionString"]);

        List<XName> variantProperties = new List<XName>()
        {
            xNS.GetName("UserName"),
            xNS.GetName("ApprovalStatus"),
            xNS.GetName("DocumentId"),
            xNS.GetName("LastModifiedTime")
        };

        store.Promote("DocumentStatus", variantProperties, null);
    }
}

谢谢!

0 个答案:

没有答案