UnhandledExceptionAction 枚举

定义

指定当异常转义工作流的根时发生的操作。Specifies the action that occurs when an exception escapes the root of a workflow.

public enum class UnhandledExceptionAction
public enum UnhandledExceptionAction
type UnhandledExceptionAction = 
Public Enum UnhandledExceptionAction
继承
UnhandledExceptionAction

字段

Abort 0

指定 WorkflowApplication 应中止工作流。Specifies that the WorkflowApplication should abort the workflow. 这会导致调用在中止进程完成时由 Aborted 返回的委托。This results in the calling the delegate returned by the Aborted bwhen the abort process is complete. 未处理的异常将用作中止原因。The unhandled exception is used as the abort reason.

Cancel 1

指定 WorkflowApplication 应安排取消根活动并恢复执行。Specifies that the WorkflowApplication should schedule the cancellation of the root activity and resume execution. 这会导致调用在取消进程完成时由 Completed 属性返回的委托。This results in a call to the delegate returned by the Completed property when the cancellation process is complete.

Terminate 2

指定 WorkflowApplication 应安排终止根活动并恢复执行。Specifies that the WorkflowApplication should schedule termination of the root activity and resume execution. 这会导致调用在终止进程完成时分配给 Completed 属性的委托。This results in a call to the delegate assigned to the Completedproperty when the termination process is complete. 未处理的异常将用作终止原因。The unhandled exception is used as the termination reason. 如果未指定 OnUnhandledException 处理程序,默认操作为 TerminateTerminate is the default action if no OnUnhandledException handler is specified.

示例

下面的示例调用了引发异常的工作流。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;
};

wfApp.Aborted = delegate(WorkflowApplicationAbortedEventArgs e)
{
    // Display the exception that caused the workflow
    // to abort.
    Console.WriteLine("Workflow {0} Aborted.", e.InstanceId);
    Console.WriteLine("Exception: {0}\n{1}",
        e.Reason.GetType().FullName,
        e.Reason.Message);
};

wfApp.Completed = delegate(WorkflowApplicationCompletedEventArgs e)
{
    if (e.CompletionState == ActivityInstanceState.Faulted)
    {
        Console.WriteLine("Workflow {0} Terminated.", e.InstanceId);
        Console.WriteLine("Exception: {0}\n{1}",
            e.TerminationException.GetType().FullName,
            e.TerminationException.Message);
    }
    else if (e.CompletionState == ActivityInstanceState.Canceled)
    {
        Console.WriteLine("Workflow {0} Canceled.", e.InstanceId);
    }
    else
    {
        Console.WriteLine("Workflow {0} Completed.", e.InstanceId);

        // Retrieve the outputs of the workflow.
        foreach (var kvp in e.Outputs)
        {
            Console.WriteLine("Name: {0} - Value {1}",
                kvp.Key, kvp.Value);
        }

        // Outputs can be directly accessed by argument name.
        // Console.WriteLine("The winner is {0}.", e.Outputs["Winner"]);
    }
};

wfApp.Run();

下面的示例调用了引发异常的工作流。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 aborted.

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 abort the workflow.
    return UnhandledExceptionAction.Abort;
};

wfApp.Aborted = delegate(WorkflowApplicationAbortedEventArgs e)
{
    // Display the exception that caused the workflow
    // to abort.
    Console.WriteLine("Workflow {0} Aborted.", e.InstanceId);
    Console.WriteLine("Exception: {0}\n{1}",
        e.Reason.GetType().FullName,
        e.Reason.Message);
};

wfApp.Completed = delegate(WorkflowApplicationCompletedEventArgs e)
{
    if (e.CompletionState == ActivityInstanceState.Faulted)
    {
        Console.WriteLine("Workflow {0} Terminated.", e.InstanceId);
        Console.WriteLine("Exception: {0}\n{1}",
            e.TerminationException.GetType().FullName,
            e.TerminationException.Message);
    }
    else if (e.CompletionState == ActivityInstanceState.Canceled)
    {
        Console.WriteLine("Workflow {0} Canceled.", e.InstanceId);
    }
    else
    {
        Console.WriteLine("Workflow {0} Completed.", e.InstanceId);

        // Outputs can be retrieved from the Outputs dictionary,
        // keyed by argument name.
        // Console.WriteLine("The winner is {0}.", e.Outputs["Winner"]);
    }
};

wfApp.Run();

下面的示例调用了引发异常的工作流。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 cancelled.

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 cancel the workflow.
    return UnhandledExceptionAction.Cancel;
};

wfApp.Aborted = delegate(WorkflowApplicationAbortedEventArgs e)
{
    // Display the exception that caused the workflow
    // to abort.
    Console.WriteLine("Workflow {0} Aborted.", e.InstanceId);
    Console.WriteLine("Exception: {0}\n{1}",
        e.Reason.GetType().FullName,
        e.Reason.Message);
};

wfApp.Completed = delegate(WorkflowApplicationCompletedEventArgs e)
{
    if (e.CompletionState == ActivityInstanceState.Faulted)
    {
        Console.WriteLine("Workflow {0} Terminated.", e.InstanceId);
        Console.WriteLine("Exception: {0}\n{1}",
            e.TerminationException.GetType().FullName,
            e.TerminationException.Message);
    }
    else if (e.CompletionState == ActivityInstanceState.Canceled)
    {
        Console.WriteLine("Workflow {0} Canceled.", e.InstanceId);
    }
    else
    {
        Console.WriteLine("Workflow {0} Completed.", e.InstanceId);

        // Outputs can be retrieved from the Outputs dictionary,
        // keyed by argument name.
        // Console.WriteLine("The winner is {0}.", e.Outputs["Winner"]);
    }
};

wfApp.Run();

注解

如果异常转义工作流的根,则将调用 OnUnhandledException 函数。The OnUnhandledException function is invoked if an exception escapes the root of the workflow. WorkflowApplicationUnhandledExceptionEventArgs 提供对异常以及指向生成异常的 Activity 的指针的访问。The WorkflowApplicationUnhandledExceptionEventArgs provides access to the exception as well as a pointer to the Activity that generated the exception. 如果未指定 OnUnhandledException 处理程序,则终止是默认操作。Terminate is the default action if no OnUnhandledException handler is specified.

适用于