Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async OnActionExecuting in ASP.NET Core's ActionFilterAttribute

ASP.NET Core's ActionFilterAttribute has these:

public virtual void OnActionExecuting(ActionExecutingContext context); public virtual void OnActionExecuted(ActionExecutedContext context); public virtual Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next); 

I need an async version of OnActionExecuting, which doesn't exist.

However I have a feeling that I can use OnActionExecutionAsync instead, as it also has an argument of ActionExecutingContext.

Am I correct that despite the name, they trigger at the same point in the process?

Also, what do I need to do with the next argument? Once I'm done with my stuff, do I simply need to call await next()?

Is that it? I'm unsure as I can't find docs for this.

like image 405
grokky Avatar asked May 15 '17 12:05

grokky


People also ask

What is Actionfilterattribute?

An action filter is an attribute that you can apply to a controller action -- or an entire controller -- that modifies the way in which the action is executed.

What is async in asp net?

The async keyword represents a hint that you can use to mark methods as task-based asynchronous methods. The combination of await, async, and the Task object makes it much easier for you to write asynchronous code in . NET 4.5. The new model for asynchronous methods is called the Task-based Asynchronous Pattern (TAP).

What is async await in ASP.NET Core?

The await operator suspends evaluation of the enclosing async method until the asynchronous operation represented by its operand completes. When the asynchronous operation completes, the await operator returns the result of the operation, if any.

Why we use async in ASP.NET Core?

Asynchronous programming allows you to write programs that don't block on each statement or instruction, meaning the computer can move on to other tasks before waiting for previous tasks to finish. As a result, asynchronous programming enables you to build applications that are more scalable and responsive.


2 Answers

Asynchronous filters work a bit differently: first execute code that must be executed before the action, call next() for the actual logic, finally add code to be executed after the action.

public async Task OnActionExecutionAsync(ActionExecutingContext context,                                           ActionExecutionDelegate next) {      // logic before action goes here      await next(); // the actual action      // logic after the action goes here } 

The documentation is here: https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/filters#implementation

like image 153
jeroenh Avatar answered Sep 17 '22 01:09

jeroenh


Asynchronous filters always take precedence over the synchronous filter implementations.

According to the Docs:

  • It's advised to implement either the synchronous or the async version of a filter interface, not both. The runtime checks first to see if the filter implements the async interface, and if so, it calls that. If not, it calls the synchronous interface's method(s). If both asynchronous and synchronous interfaces are implemented in one class, only the async method is called.

However, you can manage to have both. For instance:

public class TimestampFilter : IActionFilter, IAsyncActionFilter  {         public void OnActionExecuting(ActionExecutingContext context)         {                  context.ActionDescriptor.RouteValues["timestamp"] = DateTime.Now.ToString();         }      public void OnActionExecuted(ActionExecutedContext context)         {                  var ts = DateTime.Parse(context.ActionDescriptor. RouteValues["timestamp"]).AddHours(1).ToString();                 context.HttpContext.Response.Headers["X-EXPIRY-TIMESTAMP"] = ts;         }       public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)         {                 this.OnActionExecuting(context);                 var resultContext = await next();         this.OnActionExecuted(resultContext);         }  }  
like image 22
A. Nadjar Avatar answered Sep 18 '22 01:09

A. Nadjar