none
关于WF4持久化的问题请教 RRS feed

  • 问题

  • 我建立了一个WebSite,承载一个WCF服务,用于接收用户请求,根据请求参数(主要有流程名称,流程编号,上一流程节点处理步骤,书签名称等)决定是创建一个新的工作流实例还是从持久化数据库中加载一个工作流实例,其中Activity根据流程名称做了缓存,保证每个流程都只创建一个Activity实例。

    WCF服务主要包含两个方法:
    CreateWorkflow(string flowName, Context context)
    ContinueWorkflow(string flowName,string instanceID,string bookMark,Context context)
    上面两个方法分别用来创建新的工作流或者加载已有的工作流实例,两个方法都调用了持久化方法,持久化方法如下:
            private void SetupPersistenceStore(WorkflowApplication instance)
            {
                SqlWorkflowInstanceStore store = new SqlWorkflowInstanceStore();
                store = new SqlWorkflowInstanceStore(System.Configuration.ConfigurationManager.AppSettings["COSWF"]);
                store.InstanceCompletionAction = InstanceCompletionAction.DeleteAll;
                store.InstanceLockedExceptionAction = InstanceLockedExceptionAction.AggressiveRetry;
                store.HostLockRenewalPeriod = TimeSpan.FromSeconds(30);
                store.RunnableInstancesDetectionPeriod = TimeSpan.FromSeconds(30);
                store.MaxConnectionRetries = 256;
                store.InstanceEncodingOption = InstanceEncodingOption.GZip;
                InstanceHandle handle = store.CreateInstanceHandle();
                CreateWorkflowOwnerCommand cwoc = new CreateWorkflowOwnerCommand();
                InstanceView view = store.Execute(handle, cwoc, TimeSpan.FromSeconds(30));
                store.DefaultInstanceOwner = view.InstanceOwner;
                instance.InstanceStore = store;

                instance.PersistableIdle = (e) =>
                {
                    return PersistableIdleAction.Unload;
                };
                instance.OnUnhandledException = (arg) =>
                {
                    this.eventLogger.WriteEntry(EventLogEntryType.Error, "工作流发生未处理的异常,工作流将被终止.", arg.UnhandledException);
                    return UnhandledExceptionAction.Terminate;
                };
                instance.Unloaded = (e) =>
                {
                    DeleteWorkflowOwnerCommand dwoc = new DeleteWorkflowOwnerCommand();
                    instance.InstanceStore.Execute(handle, dwoc, TimeSpan.FromSeconds(30));
                    handle.Free();
                };

            }


    系统可以工作,但是,在做数据库持久化的时候遇到两个问题:
    1.在web.config里面配置持久化无效,只能通过C#代码设置持久化。
    2.运行一段时间以后,所有流程走完,
    [System.Activities.DurableInstancing].[IdentityOwnerTable] 这张表中却会残余很多记录,不会被清除。
    [System.Activities.DurableInstancing].[LockOwnersTable],[System.Activities.DurableInstancing].[InstancesTable] 这两张表也会残余少量记录。

    偶尔还会报两个错,不是很频繁。
    第一个错误:
    继续工作流发生异常
    System.Runtime.DurableInstancing.InstanceNotReadyException: The execution of an InstancePersistenceCommand was interrupted because the instance 'df4c3793-ee0e-426d-ba90-8109a819ccab' has not yet been persisted to the instance store.
       at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)
       at System.Runtime.DurableInstancing.InstancePersistenceContext.OuterExecute(InstanceHandle initialInstanceHandle, InstancePersistenceCommand command, Transaction transaction, 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 BOC.COS.WorkflowService.WorkFlow.ContinueWorkflow(DispatchContext context, Activity activity, ProcessResult ir)

    继续工作流发生异常
    System.Runtime.DurableInstancing.InstanceHandleConflictException: The execution of an InstancePersistenceCommand was interrupted because another valid InstanceHandle holds a lock on instance '01ff17c8-a6fd-441e-a548-ea4e510cf304', 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.
       at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)
       at System.Runtime.DurableInstancing.InstancePersistenceContext.OuterExecute(InstanceHandle initialInstanceHandle, InstancePersistenceCommand command, Transaction transaction, 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 BOC.COS.WorkflowService.WorkFlow.ContinueWorkflow(DispatchContext context, Activity activity, ProcessResult ir)


    麻烦大家帮忙看看,谢谢了。

    2013年12月26日 1:45

答案