2

我在微软论坛上发布了这个问题,但想在这里发布以增加曝光度。我到处寻找一个例子,说明我没有运气。

http://social.msdn.microsoft.com/Forums/en-US/wfprerelease/thread/8a424c0b-44f8-4670-8d64-9c7142117b55

基本上,我想使用 WorkflowApplication 以及 WorkflowIdentity 和持久性存储来完成持久延迟。我使用 WorkflowApplication 是因为我们有数百个需要动态运行的工作流,因此 WorkflowServiceHost 在我们的场景中并不真正可行。

我使用 Microsoft 提供的示例 AbsoluteDelay 作为起点,它运行良好。当我尝试将 WorkflowIdentity 传递给 WorkflowApplication 的构造函数时,问题就出现了。

然后,InstanceStore 上的 WaitForEvents 事件不会触发,因为它需要默认身份 (id 1),而不是我的新身份 (id 2)。store.Execute 行在 IdentityOwnerTable 中创建了一条记录,其中 SurrogateIdentityId 设置为 1,而不是我的身份 id。

如何告诉 InstanceStore 使用我的新身份而不是默认身份?似乎它是可以在 CreateWorkflowOwnerCommand 元数据中传递的东西。

// Create the InstanceStore
SqlWorkflowInstanceStore store = new SqlWorkflowInstanceStore(_workflowDB);

// A well known property that is needed by WorkflowApplication and the InstanceStore
private static readonly XName _workflowHostTypePropertyName = XNamespace.Get("urn:schemas-microsoft-com:System.Activities/4.0/properties").GetName("WorkflowHostType");

// Create the XName for the workflow using the workflow's unique name
XName wfHostTypeName = XName.Get(workflowName);

// Configure a Default Owner for the instance store so instances can be re-loaded from WorkflowApplication
private static InstanceHandle CreateInstanceStoreOwner(InstanceStore store, XName wfHostTypeName)
{
    InstanceHandle ownerHandle = store.CreateInstanceHandle();
    CreateWorkflowOwnerWithIdentityCommand ownerCommand = new CreateWorkflowOwnerWithIdentityCommand()
    { //changed from CreateWorkflowOwnerCommand (did not fix problem)
        InstanceOwnerMetadata =
        {
            { _workflowHostTypePropertyName, new InstanceValue(wfHostTypeName) },
            // Something here to identify the WorkflowIdentity ??
        }
    };
    store.DefaultInstanceOwner = store.Execute(ownerHandle, ownerCommand, TimeSpan.FromSeconds(30)).InstanceOwner;
    return ownerHandle;
}

private static void WaitForRunnableInstance(InstanceStore store, InstanceHandle ownerHandle)
{
    // Only fires when using default Identity
    IEnumerable<InstancePersistenceEvent> events = store.WaitForEvents(ownerHandle, TimeSpan.MaxValue);
    bool foundRunnable = false;
    // Loop through the persistence events looking for the HasRunnableWorkflow event (in this sample, it corresponds with
    // the workflow instance whose timer has expired and is ready to be resumed by the host). 
    foreach (InstancePersistenceEvent persistenceEvent in events)
    {
         if (persistenceEvent.Equals(HasRunnableWorkflowEvent.Value))
         {
             foundRunnable = true;
             break;
         }
    }
    if (!foundRunnable)
    {
        throw new ApplicationException("Unexpected: No runnable instances found in the instance store");
     }
}
4

0 回答 0