Async action filter in MVC 4

actionfilterattribute, asp.net-mvc, async-await, c#

Solution

MVC does not have an `async`-compatible action filter (but WebAPI does have one).

For now, I recommend you use blocking calls in `OnActionExecuting`. Hopefully MVC will have a better story in the future.

Update: You can vote here for the MVC team to add `async` filters.

Problem

I have an action filter that when used in certain specific conditions has to perform a web service call to ensure that the current state is valid. This initially seemed like an ideal candidate for async/await, but I have encountered a snag: Assume a request to: /Test/FilteredAction - MyCustomActionFilter begins executing - The first "await" statement is reached - TestController.FilteredAction starts executing - MyCustomActionFilter resumes executing Traditionally I would expect the action filter to resume executing and then complete before the controller action starts executing, but this does not happen. Now I assume this is because I am using: ``` public class MyCustomActionFilter : ActionFilterAttribute { public override **async** void OnActionExecuting(FilterContext context) { var foo = await WebServiceCall(); } } ``` So I think my question is: Is there an async-aware action filter class built into MVC 4, or should I just block on the calls in here?

Original source

Related problems