简体   繁体   中英

How to get the workflow activity that threw an exception

If I run a (XAML) workflow from code, eg

var activity = ActivityXamlServices.Load(@"test.xaml");
var workflowApp = new WorkflowApplication(activity);

workflowApp.Completed += eventArgs =>
{
    switch (eventArgs.CompletionState)
    {
        case ActivityInstanceState.Faulted:
            // Something went wrong, but where?
            Console.WriteLine(eventArgs.TerminationException.Message);
            break;
        case ActivityInstanceState.Closed:
            // Success
            break;
    }
};

workflowApp.Run();

Is there a way to tell which specific activity threw an exception?

I noticed that I can at least walk the entire activity tree using workflowApp.WorkflowDefinition , but I can't seem to find any indication of the activity that faulted.

I found out that it is actually quite easy. The WorkflowApplication class has an OnUnhandledException that contains information about the activity that failed.

Here is a code sample:

workflowApp.OnUnhandledException += eventArgs =>
{
    Logger.Error($"An error occured in activity '{eventArgs.ExceptionSource.DisplayName}' with ID '{eventArgs.ExceptionSource.Id}'.");
    return UnhandledExceptionAction.Terminate; 
 };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM