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
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?
STEP 01 Create new MVC Application project, named as "Async". In the File menu, click New Project. In the "New Project" dialog box, under Project types, expand Visual C#, and then click "Web". In the Name box, type "Async", then click on Ok.
ASP.NET MVC provides Action Filters for executing filtering logic either before or after an action method is called. Action Filters are custom attributes that provide declarative means to add pre-action and post-action behavior to the controller's action methods.
1 Answer. Show activity on this post. It is best for you to use async/await down the whole call stack which is possible with MVC. Mark your Action as async and then use the await keyword to wait for the results from the CsvReader method.
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.
No, there is no asynchronous filter for MVC, but it is feasible.
ASP.NET MVC filters rely on the concept of being ordered, a filter is guaranteed (if specified. See the the Order
property on the IMvcFilter
interface) to execute In a certain order if specified.
The issues that have to be addressed in light of this are:
For existing filters that are not asynchronous (at least not implemented that way because there is no other way), do they get their own task or continue off the previous one? It's an important question to answer, as you have to weigh the benefit of not continuing the next filter on another thread vs. holding onto that thread while other requests could use it.
For filters that have the same order specified, do they execute in parallel or one after the other? Making the method async opens this possibility up even more (even though it could have been done before with a non-async signature) and its definitely an architectural change which could have an impact on performance as well as expected outcome. The answer here is not so simple and would have to be based on testing and backwards compatibility.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With