2

I'm using NInject as the default container and the NInjectResolver class on an MVC4 project. Injection works great.

Now I'm trying to replace the default IActionInvoker in order to provide custom JSON serialization as shown here.

This seems to work OK if the action returns JsonResult but it chokes on Task<JsonResult> (even if I bind IActionInvoker to the default ControllerActionInvoker).

When I convert it to an async action, the action returns (instead of JSON), this text: System.Threading.Tasks.Task<System.Web.Mvc.JsonResult> - like calling ToString() on it.

How do I go about fixing this?

Community
  • 1
  • 1
georgiosd
  • 3,038
  • 3
  • 39
  • 51

1 Answers1

3

Since you are working with Task-based async, you'll have to look at injecting an instance of AsyncControllerActionInvoker.

marcind
  • 52,944
  • 13
  • 125
  • 111
  • Thanks - can you clarify however: will this also work for non-async actions? If not, should I bind one such invoke for IAsyncInvoker and a ControllerActionInvoker for IActionInvoker? In other words, how can I handle both cases with minimal effort/code? – georgiosd Sep 12 '12 at 07:09
  • 2
    You only need to register the async one, since starting with MVC 4 all controllers support async, but if you have sync methods those will work as usual. `Controller.CreateActionInvoker()` by default returns an `AsyncControllerActionInvoker`, which ultimately knows how to handle sync and async patterns (via [`AsyncActionMethodSelector.GetActionDescriptorDelegate()`](http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/c97168711de8#src%2fSystem.Web.Mvc%2fAsync%2fAsyncActionMethodSelector.cs)). – marcind Sep 12 '12 at 16:44