本節主要介紹WF的異常產生方式;WF流程結束方式;以及Throw,TryCatch,Rethrow ,TerminateWorkflow 的使用
本文例子下載:
http://files.cnblogs.com/foundation/ExceptionWorkflow.rar
本文例子說明
WF的異常產生方式與流程結束方式
WF的異常可由以下方式產生
使用[Throw Activity] 拋出異常 使用[Throw]拋出異常,不會使宿主崩潰WF流程結束方式
1. WF實例如果在運行過程中沒有出現問題,[ActivityInstanceState 實例狀態]會由[Executing 執行中] 變為 [Closed 關閉]
2. 如果流程
被調用Terminate 方法
遇到[TerminateWorkflow Activity]
產生異常時沒有用[WorkflowApplication.OnUnhandledException]
產生異常時指定[UnhandledExceptionAction.Terminate]
[ActivityInstanceState 實例狀態]會由[Executing 執行中] 變為 [faulted 有問題]
3. 如果流程
被調用Canceled 方法
產生異常時指定[UnhandledExceptionAction.Canceled ]
[ActivityInstanceState 實例狀態]會由 [Executing 執行中] 變為 [Canceled 取消]
4.如果流程
被調用Aborted 方法
產生異常時指定[UnhandledExceptionAction.Aborted ]
流程會運行失敗,並觸出發實例的Aborted
運行失敗的流程無[ActivityInstanceState 實例狀態]
5. [ActivityInstanceState 實例狀態]的 [Closed 關閉],[faulted 有問題],[Canceled 取消]都表示流程結束,並觸出發實例的 Completed
6. 流程[Aborted 失敗],流程會即時消失,觸出發實例的Aborted,不會觸出發實例的Completed
使用WorkflowApplication.OnUnhandledException 處理異常
使用實例的[OnUnhandledException]可以處理異常.
處理Code異常
用於產生中Code生異常的[Activity]
ExceptionActivity public sealed class ExceptionActivity : CodeActivity無UnhandledExceptionAction方式
//==============================================================
static void workflowCompleted(WorkflowApplicationCompletedEventArgs e)
{
System.Console.WriteLine("完成,實例編號:{0},狀態:{1}", e.InstanceId, e.CompletionState.ToString());
}
static void aborted(WorkflowApplicationAbortedEventArgs e)
{
System.Console.WriteLine("aborted ,實例編號:{1},Reason:{0}", e.Reason.Message, e.InstanceId);
}
//==============================================================
#region //處理Code異常,無[UnhandledExceptionAction]方式
static void exceptionActivityWorkflow_None()
{
WorkflowApplication instance = new WorkflowApplication(new ExceptionActivityWorkflow());
instance.Completed = new Action<WorkflowApplicationCompletedEventArgs>(workflowCompleted);
instance.Aborted = aborted;
instance.Run();
}
#endregion
當不用[WorkflowApplication.OnUnhandledException]方法處理異常時,流程正常結束.會調用實例的[completed],但狀態為[faulted]
UnhandledExceptionAction.Abort
//==============================================================
static void workflowCompleted(WorkflowApplicationCompletedEventArgs e)
{
System.Console.WriteLine("完成,實例編號:{0},狀態:{1}", e.InstanceId, e.CompletionState.ToString());
}
static void aborted(WorkflowApplicationAbortedEventArgs e)
{
System.Console.WriteLine("aborted ,實例編號:{1},Reason:{0}", e.Reason.Message, e.InstanceId);
}
//=============================================================
#region //處理Code異常,[UnhandledExceptionAction.Abort]方式
static void exceptionActivityWorkflow_Abort()
{
WorkflowApplication instance = new WorkflowApplication(new ExceptionActivityWorkflow());
instance.Completed = new Action<WorkflowApplicationCompletedEventArgs>(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;
}
#endregion
當用[UnhandledExceptionAction.Abort]方式處理異常時,流程被結束.不會調用實例的[completed],會調用實例的[Aborted]
UnhandledExceptionAction.Terminate
//==============================================================
static void workflowCompleted(WorkflowApplicationCompletedEventArgs e)
{
System.Console.WriteLine("完成,實例編號:{0},狀態:{1}", e.InstanceId, e.CompletionState.ToString());
}
static void aborted(WorkflowApplicationAbortedEventArgs e)
{
System.Console.WriteLine("aborted ,實例編號:{1},Reason:{0}", e.Reason.Message, e.InstanceId);
}
//==============================================================
#region //處理Code異常,[UnhandledExceptionAction.Terminate]方式
static void exceptionActivityWorkflow_Terminate()
{
WorkflowApplication instance = new WorkflowApplication(new ExceptionActivityWorkflow());
instance.Completed = new Action<WorkflowApplicationCompletedEventArgs>(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;
}
#endregion
當用[UnhandledExceptionAction.Abort]方式處理異常時,流程正常結束.會調用實例的[completed],但狀態為[faulted]
UnhandledExceptionAction.Cancel方式
1.如果異常出現在[CancellationScope 取消容器]中,當[UnhandledExceptionAction]方法的返回值為[Cancel]時,會調用 [CancellationScope]的[CancelHandler]部分
2.完成[CancelHandler]部分後,流程結束.會調用實例的[OnCompleted ]
具體例子見 [CancellationScope 取消容器] 主題
處理 [Throw Activity]異常
Throw 在流程中拋出異常
類名 System.Activities.Statements.Throw 文件 System.Activities.dll 結構說明 繼承 NativeActivity
無UnhandledExceptionAction方式
//==============================================================
static void workflowCompleted(WorkflowApplicationCompletedEventArgs e)
{
System.Console.WriteLine("完成,實例編號:{0},狀態:{1}", e.InstanceId, e.CompletionState.ToString());
}
static void aborted(WorkflowApplicationAbortedEventArgs e)
{
System.Console.WriteLine("aborted ,實例編號:{1},Reason:{0}", e.Reason.Message, e.InstanceId);
}
//==============================================================
#region //處理[Throw Activity]異常,無[UnhandledExceptionAction]方式
static void throwWorkflow_None()
{
WorkflowApplication instance = new WorkflowApplication(new ThrowWorkflow());
instance.Completed = new Action<WorkflowApplicationCompletedEventArgs>(workflowCompleted);
instance.Aborted = aborted;
instance.Run();
}
#endregion
當不用[WorkflowApplication.OnUnhandledException]方法處理異常時,流程正常結束.會調用實例的[completed],但狀態為[faulted]
UnhandledExceptionAction.Abort
//==============================================================
static void workflowCompleted(WorkflowApplicationCompletedEventArgs e)
{
System.Console.WriteLine("完成,實例編號:{0},狀態:{1}", e.InstanceId, e.CompletionState.ToString());
}
static void aborted(WorkflowApplicationAbortedEventArgs e)
{
System.Console.WriteLine("aborted ,實例編號:{1},Reason:{0}", e.Reason.Message, e.InstanceId);
}
//==============================================================
#region //處理[Throw Activity]異常,[UnhandledExceptionAction.Abort]方式
static void throwWorkflow_Abort()
{
WorkflowApplication instance = new WorkflowApplication(new ThrowWorkflow());
instance.Completed = new Action<WorkflowApplicationCompletedEventArgs>(workflowCompleted);
instance.OnUnhandledException = unhandledException_Abort;
instance.Aborted = aborted;
instance.Run();
}
#endregion
當用[UnhandledExceptionAction.Abort]方式處理異常時,流程被結束.不會調用實例的[completed],會調用實例的[Aborted]
UnhandledExceptionAction.Terminate
//==============================================================
static void workflowCompleted(WorkflowApplicationCompletedEventArgs e)
{
System.Console.WriteLine("完成,實例編號:{0},狀態:{1}", e.InstanceId, e.CompletionState.ToString());
}
static void aborted(WorkflowApplicationAbortedEventArgs e)
{
System.Console.WriteLine("aborted ,實例編號:{1},Reason:{0}", e.Reason.Message, e.InstanceId);
}
//==============================================================
#region //處?|理¤¨a[Throw Activity]異°¨?常?ê,[UnhandledExceptionAction.Terminate]方¤?式o?
static void throwWorkflow_Terminate()
{
WorkflowApplication instance = new WorkflowApplication(new ThrowWorkflow());
instance.Completed = new Action<WorkflowApplicationCompletedEventArgs>(workflowCompleted);
instance.OnUnhandledException = unhandledException_Terminate;
instance.Aborted = aborted;
instance.Run();
}
#endregion
當用[UnhandledExceptionAction.Abort]方式處理異常時,流程正常結束.會調用實例的[completed],但狀態為[faulted]
UnhandledExceptionAction.Cancel方式
1.如果異常出現在[CancellationScope 取消容器]中,當[UnhandledExceptionAction]方法的返回值為[Cancel]時,會調用 [CancellationScope]的[CancelHandler]部分
2.完成[CancelHandler]部分後,流程結束.會調用實例的[OnCompleted ]
具體例子見 [CancellationScope 取消容器] 主題
TryCatch 與 Catch<T> 異常捕獲
TryCatch
類名 System.Activities.Statements.TryCatch 文件 System.Activities.dll 結構說明 繼承 NativeActivity
Catch<T>
類名 System.Activities.Statements.Catch<TException> 文件 System.Activities.dll 結構說明 繼承 Object功能說明
1. [TryCatch]對[Throw Activity]與[Code 異常]都有效,如果異常被捕獲,就不會被拋出,流程會繼續向下執行
例:基本使用
1. 創建一code activity 名為[ExceptionActivity],在Execute中產生一段代碼異常
2. 創建一個繼承[ System.Exception]名為[myException]的自定義異常信息類
code activity
Rethrow 再次引發異常
類名 System.Activities.Statements.Rethrow 文件 System.Activities.dll 結構說明 繼承 NativeActivity例:基本使用
1. 創建一code activity 名 為[ExceptionActivity],在Execute中產生一段代碼異常
code activity
ExceptionActivity
public sealed class ExceptionActivity : CodeActivity
{
protected override void Execute (CodeActivityContext context)
{
int v = 1 -1;
double i = 1 / v;
}
}
工作流
宿主
//==============================================================
static void workflowCompleted (WorkflowApplicationCompletedEventArgs e)
{
System.Console.WriteLine("完成,實例編號:{0},狀態:{1}", e.InstanceId, e.CompletionState.ToString());
}
static void aborted(WorkflowApplicationAbortedEventArgs e)
{
System.Console.WriteLine("aborted ,實例編號:{1},Reason:{0}", e.Reason.Message, e.InstanceId);
}
static UnhandledExceptionAction unhandledException_Terminate(WorkflowApplicationUnhandledExceptionEventArgs e)
{
System.Console.WriteLine("unhandledException_Abort:{0}", e.UnhandledException.Message);
return UnhandledExceptionAction.Terminate;
}
//==============================================================
static void rethrowWorkflow()
{
WorkflowApplication instance = new WorkflowApplication(new RethrowWorkflow ());
instance.Completed = new Action<WorkflowApplicationCompletedEventArgs> (workflowCompleted);
instance.OnUnhandledException = unhandledException_Terminate;
instance.Aborted = aborted;
instance.Run();
}
結果
TerminateWorkflow 結束流程
類名 System.Activities.Statements.TerminateWorkflow 文件 System.Activities.dll 結構說明 繼承 NativeActivity例:基本使用
流程 宿主 #region //TerminateWorkflow例子