开发者

Getting AsyncController to work with Ninject

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 ninje开发者_Python百科ct?

cheers,


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜