WorkflowApplication.OnUnhandledException 属性

定义

获取或设置当前工作流实例遇到未处理的异常时调用的 Func<T,TResult>Gets or sets the Func<T,TResult> that is invoked when the current workflow instance encounters an unhandled exception.

public:
 property Func<System::Activities::WorkflowApplicationUnhandledExceptionEventArgs ^, System::Activities::UnhandledExceptionAction> ^ OnUnhandledException { Func<System::Activities::WorkflowApplicationUnhandledExceptionEventArgs ^, System::Activities::UnhandledExceptionAction> ^ get(); void set(Func<System::Activities::WorkflowApplicationUnhandledExceptionEventArgs ^, System::Activities::UnhandledExceptionAction> ^ value); };
public Func<System.Activities.WorkflowApplicationUnhandledExceptionEventArgs,System.Activities.UnhandledExceptionAction> OnUnhandledException { get; set; }
member this.OnUnhandledException : Func<System.Activities.WorkflowApplicationUnhandledExceptionEventArgs, System.Activities.UnhandledExceptionAction> with get, set
Public Property OnUnhandledException As Func(Of WorkflowApplicationUnhandledExceptionEventArgs, UnhandledExceptionAction)

属性值

Func<WorkflowApplicationUnhandledExceptionEventArgs,UnhandledExceptionAction>

工作流实例遇到未处理的异常时调用的委托。The delegate that is invoked when a workflow instance encounters an unhandled exception.

示例

下面的示例调用了引发异常的工作流。The following example invokes a workflow that throws an exception. 工作流未处理异常,并且 OnUnhandledException 处理程序已被调用。The exception is unhandled by the workflow and the OnUnhandledException handler is invoked. 将检查 WorkflowApplicationUnhandledExceptionEventArgs 以提供有关异常的信息,且终止工作流。The WorkflowApplicationUnhandledExceptionEventArgs are inspected to provide information about the exception, and the workflow is terminated.

Activity wf = new Sequence
{
    Activities =
     {
         new WriteLine
         {
             Text = "Starting the workflow."
         },
         new Throw
        {
            Exception = new InArgument<Exception>((env) =>
                new ApplicationException("Something unexpected happened."))
        },
        new WriteLine
         {
             Text = "Ending the workflow."
         }
     }
};

WorkflowApplication wfApp = new WorkflowApplication(wf);

wfApp.OnUnhandledException = delegate(WorkflowApplicationUnhandledExceptionEventArgs e)
{
    // Display the unhandled exception.
    Console.WriteLine("OnUnhandledException in Workflow {0}\n{1}",
        e.InstanceId, e.UnhandledException.Message);

    Console.WriteLine("ExceptionSource: {0} - {1}",
        e.ExceptionSource.DisplayName, e.ExceptionSourceInstanceId);

    // Instruct the runtime to terminate the workflow.
    return UnhandledExceptionAction.Terminate;

    // Other choices are UnhandledExceptionAction.Abort and
    // UnhandledExceptionAction.Cancel
};

wfApp.Run();

注解

当未在工作流中处理异常时,OnUnhandledExceptionWorkflowUnhandledExceptionBehavior 都将指示运行时的行为;但 WorkflowUnhandledExceptionBehavior 可以选择将已挂起的工作流保留在持久性存储区中,而 OnUnhandledException 无法做到这一点。Both OnUnhandledException and WorkflowUnhandledExceptionBehavior dictate the behavior of the runtime when an exception is not handled in the workflow; however, WorkflowUnhandledExceptionBehavior has the option of leaving a suspended workflow in the persistence store, while OnUnhandledException does not. 原因在于,对已挂起的工作流执行的操作是特定于宿主的,而 WorkflowApplication 则相反。The reason for this is that what happens to a suspended workflow is host-specific, and WorkflowApplication is not. 若要使用 WorkflowApplication 实现此功能,请创建一个具有此行为的自定义 PersistenceParticipantTo implement this functionality using WorkflowApplication, create a custom PersistenceParticipant that has this behavior.

适用于