尝试恢复 Windows 工作流时出错

本文关键字:出错 工作流 Windows 恢复 | 更新日期: 2023-09-27 18:35:05

尝试使用以下代码恢复工作流时:

    public WorkflowApplication LoadInstance(Guid instanceId)
    {
        if (this.instances.ContainsKey(instanceId))
            return this.instances[instanceId];
        WorkflowApplication instance = new WorkflowApplication(new Tarpon.Workflows.CreateContact());
        //  Create Persistable Workflow           
        SqlWorkflowInstanceStore store = new SqlWorkflowInstanceStore(ConfigurationManager.ConnectionStrings["WorkflowPersistance"].ConnectionString);
        store.HostLockRenewalPeriod = new TimeSpan(0, 0, 5);
        instance.InstanceStore = store;
        //  Load Instance
        instance.Completed += OnWorkflowCompleted;
        instance.Idle += OnIdle;
        instance.PersistableIdle += OnIdleAndPersistable;
        instance.Aborted += OnAborted;
        instance.Load(instanceId);
        //  Save instance in list of running instances
        this.instances.Add(instance.Id, instance);       // ERROR IS THROWN HERE
        return instance;
    }

我在"this.instances.Add(instance.Id,实例)":

The execution of an InstancePersistenceCommand was interrupted because the instance '9b9430b6-f182-469d-bcae-0886d546f7ea' 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 '30411662-b9b3-4250-9e2c-5aaa9895b740'.

我试图在上面的代码中降低 HostLockRenewalPeriod,并且还添加了以下代码以希望禁用实例上的锁定,但无济于事。它似乎也从未闯入下面的代码。每次我经过 Load() 方法时,我都会得到上述错误。

    public PersistableIdleAction OnIdleAndPersistable(WorkflowApplicationIdleEventArgs e)
    {
        instances.Remove(e.InstanceId);
        return PersistableIdleAction.Unload;
    }

似乎这段代码有一半时间有效,但另一半时间它无法正确恢复其工作流。有没有人知道我可以做些什么来正确移除锁,而无需重写所有这些功能?

尝试恢复 Windows 工作流时出错

请查看这篇描述持久性和 instaneStore confiuration 的博客文章。

这段代码是从帖子中复制的,我认为它可能会对您有所帮助:"

var instanceStore = new SqlWorkflowInstanceStore(connStr); 
var instanceHandle = instanceStore.CreateInstanceHandle();
var createOwnerCmd = new CreateWorkflowOwnerCommand();
var view = instanceStore.Execute(instanceHandle, createOwnerCmd, TimeSpan.FromSecond(30));
instanceStore.DefaultInstanceOwner = view.InstanceOwner; 
// Do whatever needs to be dome with multiple WorkflowApplications 
var deleteOwnerCmd = new    DeleteWorkflowOwnerCommand();
instanceStore.Execute(instanceHandle, deleteOwnerCmd, TimeSpan.FromSeconds(30));

关键是需要在开始时执行的 CreateWorkflowOwnerCommand。当您使用 CreateWorkflowOwnerCommand 时,请确保不要忘记 DeleteWorkflowOwnerCommand,否则所有工作流都将被所有者锁定,并且无法由另一个 SqlWorkflowInstanceStore 重新加载

我在这里看不到instance.Runinstance.ResumeBookmark,您需要它来触发任何与执行相关的事件,例如 PersistableIdle。

实际上

,当我在开发中看到此错误时,它只是意味着我需要清理我的持久性数据库。 可以使用已存在的存储过程来删除暂留的工作流。

尝试创建一个WorkflowIdleBehavior对象并将其TimeToUnload设置为零。有关更多详细信息,请参阅此处。