恢复工作流程的例外情况

时间:2013-12-02 20:38:05

标签: workflow-foundation-4

在_wfApp.Load(id)中运行以下resume函数时;出现以下异常

    public static void Resume(Guid id, Activity activity, string bookmarkName, object bookmarkvalue)
    {

        WorkflowApplication _wfApp = SetUpInstance(activity, null);
        _wfApp.Load(id);

        _wfApp.ResumeBookmark(bookmarkName, bookmarkvalue);


    }

    private static WorkflowApplication SetUpInstance(Activity wfActivity, IDictionary<string, object> parameters)
    {
        Guid id = Guid.Empty;


        if (parameters != null)
        {
            _wfApp = new WorkflowApplication(wfActivity, parameters);
        }
        else
        {
            _wfApp = new WorkflowApplication(wfActivity);
        }

        _wfApp.InstanceStore = GetInstanceStore();
        _wfApp.SynchronizationContext = new SynchronousSynchronizationContext();
        _wfApp.OnUnhandledException = OnUnhandledException;
        _wfApp.Completed = OnWorkflowCompleted;
        _wfApp.Idle = OnWorkflowIdle;
        _wfApp.PersistableIdle = OnPersistableIdle;
        _wfApp.Unloaded = OnWorkflowUnloaded;

        // add a tracking participant
        _wfApp.Extensions.Add(new SaveAllEventsToTestFileTrackingParticipant());

        //ChangedStateNotifier extensionNotifier = new ChangedStateNotifier();
        //extensionNotifier.Notification += delegate(object sender, HostNotifyEventArgs e)
        //{
        //    if (ChangedState != null)
        //    {
        //        ChangedState(e.WorkflowId, e.WorkflowStatus);
        //    }
        //};

        //wfApp.InstanceStore = store;
        //wfApp.Extensions.Add(extensionNotifier);

        return _wfApp;
    }

    private static PersistableIdleAction OnPersistableIdle(WorkflowApplicationIdleEventArgs e)
    {
        return PersistableIdleAction.Persist;
    }

    private static void OnWorkflowUnloaded(WorkflowApplicationEventArgs e)
    {
        //_syncEvent.Set();
    }

    private static void OnWorkflowIdle(WorkflowApplicationIdleEventArgs e)
    {

    }

    private static void OnWorkflowCompleted(WorkflowApplicationCompletedEventArgs e)
    {
        //_syncEvent.Set();
    }

    private static UnhandledExceptionAction OnUnhandledException(WorkflowApplicationUnhandledExceptionEventArgs e)
    {
        return UnhandledExceptionAction.Terminate;
    }

    private static SqlWorkflowInstanceStore GetInstanceStore()
    {
        //if (_Store == null)
        //{
            _Store = new SqlWorkflowInstanceStore(persistenceConnectionString);
            _Store.InstanceCompletionAction = InstanceCompletionAction.DeleteNothing;

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

            // Do whatever needs to be dome with multiple WorkflowApplications

            var deleteOwnerCmd = new DeleteWorkflowOwnerCommand();
            _Store.Execute(instanceHandle, deleteOwnerCmd, TimeSpan.FromSeconds(30));
        //}
        return _Store;
    }

异常消息

System.Runtime.DurableInstancing.InstanceHandleConflictException was unhandled by user code
  HResult=-2146233088
  Message=The execution of an InstancePersistenceCommand was interrupted because another valid InstanceHandle holds a lock on instance '9219f68e-a952-4a6b-8d65-3e674aa39421', indicating that a non-stale copy of the instance is already loaded. The loaded copy of the instance and its associated InstanceHandle should be used or unloaded.
  Source=System.ServiceModel.Internals
  StackTrace:
       at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)
       at System.Runtime.DurableInstancing.InstancePersistenceContext.OuterExecute(InstanceHandle initialInstanceHandle, InstancePersistenceCommand command, Transaction transaction, TimeSpan timeout)
       at System.Runtime.DurableInstancing.InstanceStore.Execute(InstanceHandle handle, InstancePersistenceCommand command, TimeSpan timeout)
       at System.Activities.WorkflowApplication.PersistenceManager.Load(TimeSpan timeout)
       at System.Activities.WorkflowApplication.LoadValues(PersistenceManager persistenceManager, TimeoutHelper timeoutHelper, Boolean loadAny)
       at System.Activities.WorkflowApplication.LoadCore(DynamicUpdateMap updateMap, TimeoutHelper timeoutHelper, Boolean loadAny, IDictionary`2 values)
       at System.Activities.WorkflowApplication.Load(Guid instanceId, TimeSpan timeout)
       at System.Activities.WorkflowApplication.Load(Guid instanceId)
       at WebApplication1.WorkflowManager.Resume(Guid id, Activity activity, String bookmarkName, Object bookmarkvalue) in c:\Users\amf\Documents\Visual Studio 2012\Projects\WebApplication1\WebApplication1\WorkflowManager.cs:line 64
       at WebApplication1.Manager_Approval.btnSubmit_Click(Object sender, EventArgs e) in c:\Users\amf\Documents\Visual Studio 2012\Projects\WebApplication1\WebApplication1\ManagerApproval.aspx.cs:line 19
       at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
       at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: 

1 个答案:

答案 0 :(得分:0)

您正在删除自己的实例锁。

卸下:

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

此代码用于获取锁定实例的所有权。

您缺少instanceHandle.Free();

要加载实例,您可以使用实例存储首先获取它:

var instance = WorkflowApplication.GetInstance(instanceId, store);
var wf = wfActivity; //todo reslove your workflow over instance.DefinitionIdentity
var wfApp = new WorkflowApplication(wf, instance.DefinitionIdentity);

//todo configure wfApp with Event delegates and Extensions

wfApp.Load(instance);

wfApp.ResumeBookmark(bookmarkName, bookmarkvalue);