Hi,
I have a simple action filter like that:
``` C#
public class AbpUowActionFilter : IAsyncActionFilter
{
...
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
using (var uow = _unitOfWorkManager.Begin())
{
await next();
await uow.CompleteAsync();
}
}
}
```
What I want is simple: If no exception occurs, then I complete the unitofwork/transaction.
The problem is that: Exceptions are internally handled by MVC (https://github.com/aspnet/Mvc/blob/5339a3e0cee469b856c11344613f8ab8c3c802ba/src/Microsoft.AspNetCore.Mvc.Core/Internal/ControllerActionInvoker.cs#L386) and my CompleteAsync code is executed even there is an exception after this filter.
I checked ActionExecutingContext if there is an Exception property that I can check to know if there is an exception. Unfortunately, there is not such a property. ActionExecutedContext has such a property (https://github.com/aspnet/Mvc/blob/ee2cfa1963ee7f8195130d41330f94d4622ca89a/src/Microsoft.AspNetCore.Mvc.Abstractions/Filters/ActionExecutedContext.cs#L47) but I want to implement IAsyncActionFilter, not IActionFilter. Is there a possibility to know if there is an exception in IAsyncActionFilter?
In an IAsyncActionFilter
calling next()
returns an ActionExecutedContext
. So:
``` C#
public class AbpUowActionFilter : IAsyncActionFilter
{
...
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
using (var uow = _unitOfWorkManager.Begin())
{
var executed = await next();
if (executed.Exception != null && !executed.ExceptionHandled)
{
await uow.CompleteAsync();
}
else
{
await uow.RollbackAsync();
}
}
}
}
```
Thank you. You can close this issue since I got my answer :)
... except commit on no exception regardless of if the exception is handled already or not.
if (executed.Exception == null)
{
// commit
}
else
{
// rollback
}
Thanks, updated
Most helpful comment
In an
IAsyncActionFilter
callingnext()
returns anActionExecutedContext
. So:``` C#
public class AbpUowActionFilter : IAsyncActionFilter
{
...
}
```