views:

39

answers:

4

I am trying to convert some actions within a controller to run asynchronously in an mvc project that is using ninject for dependency injection. I'm following the steps by inheriting AsyncController and changing the methods that correspond to the 'X' action to 'XAsync' and 'XCompleted' but the async action is not getting resolved. I'm confident that the issue has to do with ninject. I have tried to explicitly set ninject's Controller Action Invoker to 'AsyncControllerActionInvoker':

Bind<IActionInvoker>().To<AsyncControllerActionInvoker>().InSingletonScope();

but no luck. Has anyone managed to get Async actions working with ninject?

cheers,

A: 

After much search i realized in addition to this, each controller needs to explicitly be set to use an Action Invoker that supports Async Actions.

ambog36
A: 

can you post an example how you fixed it? i am having same issue, ninject and async controller not playing nice.

thanks zm

+1  A: 

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.

ambog36
A: 

Thanks.

i opened up another thread and i got this response from dave, he also suggested a similar approach.

http://stackoverflow.com/questions/3823899/how-to-use-asynccontroller-in-mvc-application-using-ninject-for-di/3840916#3840916

Thanks

zm