//Private #region 获取持久化模式 —— static InstanceCompletionAction GetInstanceCompletionAction() /// <summary> /// 获取持久化模式 /// </summary> /// <returns>持久化模式</returns> private static InstanceCompletionAction GetInstanceCompletionAction() { PersistenceMode persistenceMode = WorkflowExtension.GetPersistenceMode(); int enumValue = (int)persistenceMode; InstanceCompletionAction instanceCompletionAction = (InstanceCompletionAction)enumValue; return(instanceCompletionAction); }
/// <summary> /// 创建工作流应用程序代理构造器 /// </summary> /// <param name="workflowDefinition">工作流定义</param> /// <param name="parameters">参数字典</param> /// <param name="definitionIdentity">工作流定义标识</param> public WorkflowApplicationProxy(Activity workflowDefinition, IDictionary <string, object> parameters = null, WorkflowIdentity definitionIdentity = null) { //创建工作流应用程序 if (workflowDefinition == null) { throw new ArgumentNullException(nameof(workflowDefinition), "工作流定义不可为空!"); } if (parameters == null && definitionIdentity == null) { this.WorkflowApplication = new WorkflowApplication(workflowDefinition); } if (parameters == null && definitionIdentity != null) { this.WorkflowApplication = new WorkflowApplication(workflowDefinition, definitionIdentity); } if (parameters != null && definitionIdentity == null) { this.WorkflowApplication = new WorkflowApplication(workflowDefinition, parameters); } if (parameters != null && definitionIdentity != null) { this.WorkflowApplication = new WorkflowApplication(workflowDefinition, parameters, definitionIdentity); } //设置工作流持久化存储 InstanceCompletionAction instanceCompletionAction = GetInstanceCompletionAction(); SqlWorkflowInstanceStore instanceStore = new SqlWorkflowInstanceStore() { ConnectionString = _WorkflowPersistenceConnectionString, InstanceCompletionAction = instanceCompletionAction }; this.WorkflowApplication.InstanceStore = instanceStore; //设置空闲时卸载持久化 this.WorkflowApplication.PersistableIdle = eventArgs => { //只有书签才持久化 if (eventArgs.Bookmarks.Any()) { return(PersistableIdleAction.Unload); } return(PersistableIdleAction.None); }; //设置书签卸载事件 this.WorkflowApplication.Idle = eventArgs => { if (eventArgs.Bookmarks.Any()) { string[] bookmarkNames = eventArgs.Bookmarks.Select(x => x.BookmarkName).ToArray(); this.BookmarksUnloadedEvent?.Invoke(this.WorkflowApplication.Id, bookmarkNames, eventArgs); } }; //设置工作流实例完成事件 this.WorkflowApplication.Completed = eventArgs => { this.CompletedEvent?.Invoke(this.WorkflowApplication.Id, eventArgs); }; //异常处理 this.WorkflowApplication.OnUnhandledException = eventArgs => { if (eventArgs.ExceptionSource is BookmarkActivity bookmarkActivity) { //设置书签异常事件 Guid workflowInstanceId = this.WorkflowApplication.Id; string bookmarkName = bookmarkActivity.GetType().FullName; Exception unhandledException = eventArgs.UnhandledException; this.BookmarkExceptionEvent?.Invoke(workflowInstanceId, bookmarkName, unhandledException, eventArgs); } //记录日志 LogException(eventArgs); //中止工作流 return(UnhandledExceptionAction.Abort); }; }