1、使用WorkflowApplication.OnUnhandledException 处理Code异常 ,用于产生异常的Activity:
public class ExceptionActivity:CodeActivity { protected override void Execute(CodeActivityContext context) { ; / a; } }
-无UnhandledExceptionAction方式
static void ExceptionActivityWorkflow_None() { WorkflowApplication instance = new WorkflowApplication(new ExceptionActivityWorkflow()); instance.Completed = workflowCompleted; instance.Aborted = aborted; instance.Run(); } private static void aborted(WorkflowApplicationAbortedEventArgs obj) { Console.WriteLine("aborted,实例编号:{1},Reason:{0}", obj.Reason.Message, obj.InstanceId); } static void workflowCompleted(WorkflowApplicationCompletedEventArgs e) { Console.WriteLine("完成,实例编号:{0},状态:{1}", e.InstanceId, e.CompletionState.ToString()); }
不用[OnUnhandledException]方法处理异常时,流程正常结束.调用实例的[completed],状态为[faulted]
-处理Code异常,[UnhandledExceptionAction.Abort]方式
static void ExceptionActivityWorkflow_Abort() { WorkflowApplication instance = new WorkflowApplication(new ExceptionActivityWorkflow()); instance.Completed =workflowCompleted; instance.OnUnhandledException = unhandledException_Abort; instance.Aborted = aborted; instance.Run(); } static UnhandledExceptionAction unhandledException_Abort(WorkflowApplicationUnhandledExceptionEventArgs e) { System.Console.WriteLine("unhandledException_Abort:{0}", e.UnhandledException.Message); return UnhandledExceptionAction.Abort; } private static void aborted(WorkflowApplicationAbortedEventArgs obj) { Console.WriteLine("aborted,实例编号:{1},Reason:{0}", obj.Reason.Message, obj.InstanceId); } static void workflowCompleted(WorkflowApplicationCompletedEventArgs e) { Console.WriteLine("完成,实例编号:{0},状态:{1}", e.InstanceId, e.CompletionState.ToString()); }
用[UnhandledExceptionAction.Abort]方式处理异常时,流程被结束.不会调用实例的[completed],会调用实例的[Aborted]
-UnhandledExceptionAction.Terminate
static void exceptionActivityWorkflow_Terminate() { WorkflowApplication instance = new WorkflowApplication(new ExceptionActivityWorkflow()); instance.Completed = workflowCompleted; instance.OnUnhandledException = unhandledException_Terminate; instance.Aborted = aborted; instance.Run(); } static UnhandledExceptionAction unhandledException_Terminate(WorkflowApplicationUnhandledExceptionEventArgs e) { System.Console.WriteLine("unhandledException_Abort:{0}", e.UnhandledException.Message); return UnhandledExceptionAction.Terminate; }
用[UnhandledExceptionAction.Terminate]方式处理异常时,流程正常结束.会调用实例的[completed],但状态为[faulted]