I'm working in an MVC5 application that will be handling some intense IO operations. So we are converting methods to use the async/await TPL stuff.
Prior to the async/await changes I registered a custom IAsyncActionInvoker which changes the Json ActionResult to a JsonNet ActionResult like in this example:
Using JSON.NET as the default JSON serializer in ASP.NET MVC 3 - is it possible?
The trouble comes in when changing to use async/await Task where the JsonNet ActionResult is never executed. Does anyone know how to maintain this implementation while moving to async/await Task ?
public class CustomActionInvoker : AsyncControllerActionInvoker
{
protected override ActionResult InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary<string, object> parameters)
{
ActionResult invokeActionMethod = base.InvokeActionMethod(controllerContext, actionDescriptor, parameters);
if (invokeActionMethod is JsonResult)
{
return new JsonNetResult(invokeActionMethod as JsonResult);
}
return invokeActionMethod;
}
}
public class HomeController : Controller
{
// THIS WORKS!
public ActionResult Test()
{
return Json(new
{
Property1 = "blah",
Property2 = "test"
}, JsonRequestBehavior.AllowGet);
}
// THIS DOES NOT FIRE MY CUSTOM ACTION INVOKER
// IT RUNS THE DEFAULT
public async Task<ActionResult> Test()
{
await Task.Run(() =>
{
Thread.Sleep(1000);
});
return Json(new
{
Property1 = "blah",
Property2 = "test"
}, JsonRequestBehavior.AllowGet);
}
}
Any help would be greatly appreciated guys!