gpt4 book ai didi

c# - WF4 : Workflow stay locked

转载 作者:行者123 更新时间:2023-11-30 22:24:23 26 4
gpt4 key购买 nike

我有一个使用 WorkflowApplication 在 IIS 中托管 WF4 工作流的应用程序

工作流由用户定义(使用重新托管的工作流设计器)并且 xml 存储在数据库中。然后,根据使用该应用程序的用户操作,在数据库中选择一个 xml 并创建/恢复工作流。

我的问题是:当工作流到达书签并闲置时,它会保持锁定状态不同的时间。然后,如果用户尝试对这个工作流程进行另一个操作,我会得到这个异常:

The execution of an InstancePersistenceCommand was interrupted because the instance '52da4562-896e-4959-ae40-5cd016c4ae79' is locked by a different instance owner. This error usually occurs because a different host has the instance loaded. The instance owner ID of the owner or host with a lock on the instance is 'd7339374-2285-45b9-b4ea-97b18c968c19'.

现在是写一段代码的时候了

当我的工作流空闲时,我指定它应该被卸载:

private PersistableIdleAction handlePersistableIdle(WorkflowApplicationIdleEventArgs arg)
{
this.Logger.DebugFormat("Workflow '{1}' is persistableIdle on review '{0}'", arg.GetReviewId(), arg.InstanceId);
return PersistableIdleAction.Unload;
}

对于我需要的 WorkflowApplication,我创建了一个新的 SqlWorkflowInstanceStore:

var store = new SqlWorkflowInstanceStore(this._connectionString);
store.RunnableInstancesDetectionPeriod = TimeSpan.FromSeconds(5);
store.InstanceLockedExceptionAction = InstanceLockedExceptionAction.BasicRetry;

这是我的 WorkflowApplication 是如何创建的

WorkflowApplication wfApp = new WorkflowApplication(root.RootActivity);
wfApp.Extensions.Add(...);
wfApp.InstanceStore = this.createStore();
wfApp.PersistableIdle = this.handlePersistableIdle;
wfApp.OnUnhandledException = this.handleException;
wfApp.Idle = this.handleIdle;
wfApp.Unloaded = this.handleUnloaded;
wfApp.Aborted = this.handleAborted;
wfApp.SynchronizationContext = new CustomSynchronizationContext();
return wfApp;

然后我调用 Run 方法启动它。一些解释:
- root.RootActivity:它是从存储在数据库中的工作流 XML 创建的事件
- CustomSynchronizationContext:处理授权的同步上下文
- 在卸载工作流时记录的 handleUnloaded 方法中,我看到工作流在下一个用户操作之前正确卸载,但似乎工作流在卸载后保持锁定状态(?)

然后,稍后,当我需要恢复工作流时,我会以相同的方式创建工作流,然后调用:

wfApp.Load(workflowInstanceId);

抛出上面指定的“锁定”异常。如果我等几分钟,然后重试,效果很好。

我读了一篇 here 的帖子,说我们需要设置一个所有者。所以我也尝试过使用静态 SqlWorkflowInstanceStore 和使用此代码设置的所有者:

if (_sqlWorkflowInstanceStore != null)
return _sqlWorkflowInstanceStore;

lock (_mutex)
{
if (_sqlWorkflowInstanceStore != null)
return _sqlWorkflowInstanceStore;

// Configure store
_sqlWorkflowInstanceStore = new SqlWorkflowInstanceStore(this._connectionString);
_sqlWorkflowInstanceStore.RunnableInstancesDetectionPeriod = TimeSpan.FromSeconds(5);
_sqlWorkflowInstanceStore.InstanceLockedExceptionAction = InstanceLockedExceptionAction.BasicRetry;

// Set owner - Store will be read-only beyond this point and will not be configurable anymore
var handle = _sqlWorkflowInstanceStore.CreateInstanceHandle();
var view = _sqlWorkflowInstanceStore.Execute(handle, new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(5));
handle.Free();

_sqlWorkflowInstanceStore.DefaultInstanceOwner = view.InstanceOwner;
}

return _sqlWorkflowInstanceStore;

但是我遇到了这种异常:

The execution of an InstancePersistenceCommand was interrupted because the instance owner registration for owner ID '9efb4434-8560-469f-9d03-098a2d48821e' has become invalid. This error indicates that the in-memory copy of all instances locked by this owner have become stale and should be discarded, along with the InstanceHandles. Typically, this error is best handled by restarting the host.

有谁知道如何确保在卸载工作流时立即释放对工作流的锁定?我看到一些 post 使用 WorkflowServiceHost(使用 WorkflowIdleBehavior)来执行此操作,但这里我使用 WorkflowServiceHost,我使用 WorkflowApplication

感谢您的帮助!

最佳答案

我怀疑问题出在 SqlWorkflowInstanceStore 的 InstanceOwner 上。它不会被删除,因此工作流需要等待前一个的所有权超时。

创建实例所有者

var instanceStore = new SqlWorkflowInstanceStore(connStr);

var instanceHandle = instanceStore.CreateInstanceHandle();
var createOwnerCmd = new CreateWorkflowOwnerCommand();
var view = instanceStore.Execute(instanceHandle, createOwnerCmd, TimeSpan.FromSeconds(30));
instanceStore.DefaultInstanceOwner = view.InstanceOwner;

删除实例所有者

var deleteOwnerCmd = new DeleteWorkflowOwnerCommand();
instanceStore.Execute(instanceHandle, deleteOwnerCmd, TimeSpan.FromSeconds(30));

另一个可能的问题是,当工作流中止时,未调用 Unloaded 回调。

关于c# - WF4 : Workflow stay locked,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12797975/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com