Essentially the problem i was facing was that the default action invoker that is used by ninject doesnt support async actions and when you try to set the action invoker in a controller the default ninjectControllerFactory overrides it. I took the following steps to fix the problem:
1.In the injection mapping i added the following association:
Bind<IActionInvoker>().To<AsyncControllerActionInvoker>().InSingletonScope();
2.I created a custom controller factory that is basically ninject's controller factory with the only difference being that it doesn't overwrite the action invoker.
public class CustomNinjectControllerFactory : DefaultControllerFactory {
/// <summary>
/// Gets the kernel that will be used to create controllers.
/// </summary>
public IKernel Kernel { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="NinjectControllerFactory"/> class.
/// </summary>
/// <param name="kernel">The kernel that should be used to create controllers.</param>
public CustomNinjectControllerFactory(IKernel kernel) {
Kernel = kernel;
}
/// <summary>
/// Gets a controller instance of type controllerType.
/// </summary>
/// <param name="requestContext">The request context.</param>
/// <param name="controllerType">Type of controller to create.</param>
/// <returns>The controller instance.</returns>
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) {
if (controllerType == null) {
// let the base handle 404 errors with proper culture information
return base.GetControllerInstance(requestContext, controllerType);
}
var controller = Kernel.TryGet(controllerType) as IController;
if (controller == null)
return base.GetControllerInstance(requestContext, controllerType);
var standardController = controller as Controller;
if (standardController != null && standardController.ActionInvoker == null)
standardController.ActionInvoker = CreateActionInvoker();
return controller;
}
/// <summary>
/// Creates the action invoker.
/// </summary>
/// <returns>The action invoker.</returns>
protected virtual NinjectActionInvoker CreateActionInvoker() {
return new NinjectActionInvoker(Kernel);
}
}
3.In OnApplicationStarted() method I set the controller factory to my custom one:
ControllerBuilder.Current.SetControllerFactory(new customNinjectControllerFactory(Kernel));`
Hope this helps.