Transferring information to the middleware from the controller

I installed middleware in my main asp.net application to automatically transfer controller actions to a transaction.

public async Task Invoke(HttpContext context, MyDataContext dbContext)
{
    if (context.Request.Method == "GET")
    {
        await _next(context);
    }
    else
    {
        using (var transaction = await dbContext.Database.BeginTransactionAsync())
        {
            try
            {
                await _next(context);
                transaction.Commit();
            }
            catch (Exception)
            {
                transaction.Rollback();
                throw;
            }
        }
    }
}

However, I have one controller method (possibly more in the future) that should not be executed in a transaction. What would be the best way to make this controller action β€œabandon” middleware functionality?

, [NoTransaction], . , , Invoke() , , ( , .) ?

+4
1

, .

, ActionFilterAttribute. OnActionExecuting OnActionExecuted, .

, . [CustomFilter], "CustomFilterAttribute".

, ActionFilter ServiceFilter. , . ServiceFilter :

[ServiceFilter(typeof(CustomFilter))]

: https://damienbod.com/2015/09/15/asp-net-5-action-filters/

+1

Source: https://habr.com/ru/post/1660859/


All Articles