I have created a custom ActionFilter that goes like the following:
public override void OnActionExecuting(ActionExecutingContext context){
SomeMethod().Wait();
}
SomeMethod contains a awaitable call and therefore my only solution so far was to put the ‘.Wait()’ after it. Not pretty, but hey – ‘it works just fine for the purpose’…


Now I want to add an if-statement inside the OnActionExecuting-method (since I dont want to continue to the controller dependent of what the awaitable call returns inside SomeMethod()), so it goes somewhat like the following:
public override void OnActionExecuting(ActionExecutingContext context){
var result = SomeMethod().Wait(); // not possible, since you cannot assign void to an implicitly-typed local variable
if(result) {
// do this...
}
else {
// do that...
}
}

This is not possible – and from what I’ve found so far, the MVC-OnActionExecuting don’t have async…
Can this be solved somehow? Sorry if it is obvious, but this i the first time working with these kinds of action filters.
an option would be:
SomeMethod.Wait();
var result = SomeMethod.Result;
But I’m not sure, if that is a wrong way of bypassing it…?
This is a wrong way of doing it, yes. Tasks should be awaited. The exceptions to this are vanishingly few.
What version of ASP.NET are you using? Is it a particularly old one? ActionFilterAttribute has had asynchronous overloads since ASP.NET Core 1.0.
C# devs
null reference exceptions

source