让 AsyncController 与 Ninject 一起工作

发布于 2024-09-17 11:08:17 字数 413 浏览 1 评论 0原文

我正在尝试将控制器中的某些操作转换为在使用 ninject 进行依赖项注入的 mvc 项目中异步运行。我按照以下步骤继承 AsyncController 并将与“X”操作对应的方法更改为“XAsync”和“XCompleted”,但异步操作未得到解决。我确信这个问题与 ninject 有关。我尝试将 ninject 的控制器操作调用器显式设置为 'AsyncControllerActionInvoker':

Bind().To().InSingletonScope();

但没有运气。有没有人设法让异步操作与 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 ninject?

cheers,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七色彩虹 2024-09-24 11:08:17

本质上,我面临的问题是 ninject 使用的默认操作调用程序不支持异步操作,当您尝试在控制器中设置操作调用程序时,默认的 ninjectControllerFactory 会覆盖它。我采取了以下步骤来解决问题:

1.在注入映射中,我添加了以下关联:

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

2.我创建了一个自定义控制器工厂,它基本上是 ninject 的控制器工厂,唯一的区别是它不会覆盖操作调用程序。

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.在 OnApplicationStarted() 方法中,我将控制器工厂设置为我的自定义工厂:

ControllerBuilder.Current.SetControllerFactory(new customNinjectControllerFactory(Kernel));`

希望这会有所帮助。

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.

丶情人眼里出诗心の 2024-09-24 11:08:17

经过大量搜索后,我意识到除此之外,每个控制器都需要明确设置为使用支持异步操作的操作调用程序。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文