WorkflowApplication.OnUnhandledException Proprietà

Definizione

Ottiene o imposta l'oggetto Func<T,TResult> richiamato quando l'istanza del flusso di lavoro corrente rileva un'eccezione non gestita.

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)

Valore della proprietà

Delegato richiamato quando un'istanza del flusso di lavoro rileva un'eccezione non gestita.

Esempio

Nell'esempio seguente viene richiamato un flusso di lavoro che genera un'eccezione. L'eccezione non viene gestita dal flusso di lavoro e viene richiamato il gestore OnUnhandledException. Il WorkflowApplicationUnhandledExceptionEventArgs viene controllato per fornire informazioni sull'eccezione e il flusso di lavoro viene terminato.

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();

Commenti

Sia OnUnhandledException che WorkflowUnhandledExceptionBehavior dettano il comportamento del runtime quando un'eccezione non viene gestita nel flusso di lavoro. Tuttavia, WorkflowUnhandledExceptionBehavior ha la possibilità di uscire da un flusso di lavoro sospeso nell'archivio di persistenza, mentre OnUnhandledException non lo è. Il motivo è che ciò che accade a un flusso di lavoro sospeso è specifico dell'host e WorkflowApplication non è. Per implementare questa funzionalità usando WorkflowApplication, creare un oggetto personalizzato PersistenceParticipant con questo comportamento.

Si applica a