Search code examples
c#workflow-foundation-4func

How do I use a delegate with WorkflowApplication.OnUnhandledException


WF4 uses 4 Action that I can use to delegate to methods. Like this.

_workflowApplication.Completed = delegate(WorkflowApplicationCompletedEventArgs e) { WorkflowApplicationCompleted(e); };

However there are also 2 Func. I can see how to use them inline but I want them to delegate to a method.

I tried:

_workflowApplication.PersistableIdle = delegate(WorkflowApplicationIdleEventArgs e) { WorkflowApplicationPersistableIdle(e); };

but I have no return statement.

I tried:

_workflowApplication.OnUnhandledException = (returnValue) => WorkflowApplicationOnUnhandledException(e, returnValue);

but e does not resolve.

How can I delegate to a method?


Solution

  • private void SetExceptionHandler(WorkflowApplication app)
    {
        app.OnUnhandledException = x => HandleTheUnhandled(x);
    }
    
    private UnhandledExceptionAction HandleTheUnhandled(
        WorkflowApplicationUnhandledExceptionEventArgs args)
    {
        //some logic
        return UnhandledExceptionAction.Abort;
    }
    

    Or

    app.OnUnhandledException = x => 
    {
        // some logic
        return UnhandledExceptionAction.Abort;
    };
    

    Or, if no logic is required,

    app.OnUnhandledException = x => UnhandledExceptionAction.Abort;
    

    Lambdas. Learn them. Love them.