Closed chucklu closed 5 years ago
The version of web api I am using is 5.2.7
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.7" targetFramework="net472" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.7" targetFramework="net472" />
<package id="Microsoft.AspNet.WebApi.Owin" version="5.2.7" targetFramework="net472" />
Find the problem caused by the wrong usage of async keyword in public override async void OnActionExecuting(HttpActionContext actionContext)
.
Previously, I have an await statement in my method string requestBody = await request.Content.ReadAsStringAsync();
, I removed it and forgot to remove the async keyword from method.
two alternative solutions: 1.remove the async keyword and no need to inherit CustomActionFilterAttribute 2.do not remove async keyword and await the OnActionExecuting method in CustomActionFilterAttribute's OnActionExecutingAsync method.
I am using the ActionFilterAttribute to implement my model validation as introduced in Model Validation in ASP.NET Web API. I did security hash check for my parameters, and exception happened here
However, the global exception handler did not catch this exception and write a text log as expected. Inherit from ExceptionHandler to write txt log.
When I check the source code of ActionFilterAttribute, I find code like this:
I guess you might hide the exception by catch block, so I inherit the ActionFilterAttribute and override the OnActionExecutingAsync method.
Then I make ValidateModelAttribute inherit from the new created CustomActionFilterAttribute, then I try to debug the exception.
Before execute the OnActionExecuting in ValidateModelAttribute, it first execute OnActionExecutingAsync in CustomActionFilterAttribute. And the exception throwed from OnActionExecuting of ValidateModelAttribute, and it supposed to be caught in OnActionExecutingAsync of CustomActionFilterAttribute.
However, when I hit F11 to debug, the exception just disappeared and finish the
OnActionExecuting(actionContext)
. How could it be possible? Who knows something about this weird thing?