MAQ
MAQ

Reputation: 443

OnExceptionAspect with Async/Await

I want to use OnExceptionAspect from PostSharp. Want to know whether this aspect support Async methods or not?

Can anyone suggest links or sample code?

Thanks.

Upvotes: 2

Views: 434

Answers (1)

AlexD
AlexD

Reputation: 5101

To enable full compatibility of OnExceptionAspect with async methods, you need to set the ApplyToStateMachine property to true, for example in your aspect's constructor.

[Serializable]
public class CustomAspect : OnExceptionAspect
{
    public CustomAspect()
    {
        ApplyToStateMachine = true;
    }
    // ...
}

There's more information about using OnMethodBoundaryAspect with async methods in documentation and it is also applicable to OnExceptionAspect.

One important caveat is that you cannot change the exception's flow behavior in async methods. So, you can't, for example, ignore the exception being thrown, while you still can do some logging or processing on it.

Upvotes: 3

Related Questions