如何配置 Ninject 以便它为每个控制器创建一个实例 [英] How to configure Ninject so that it creates a single instance per Controller

查看:18
本文介绍了如何配置 Ninject 以便它为每个控制器创建一个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 Ninject 用于 MVC5 应用程序,我想知道是否有任何方法可以配置它以便为每个 Controller 实例化一个对象(我不确定它是否与每个请求相同),在控制器的任何 Action 中使用该实例,一旦 Action 完成释放该对象使用的所有资源.

I'm trying to use Ninject for an MVC5 application and I want to know if there's any way I can configure it so that instantiate a single object per Controller (I'm not sure if it the same as per request), use that instance in any Action of the controller and once the Action has finished release all the resources that the object utilized.

我希望 Ninject 在我的控制器中注入的类实现了 IDisposable 接口.

The class that I want Ninject to inject in my controllers implements the IDisposable interface.

到目前为止,我已经尝试过:

So far I have tried this:

  private static void RegisterServices(IKernel kernel)
  {
     kernel.Bind<IUnitOfWork>().To<SqlUnitOfWork>();
  }

但是 Dispose 方法永远不会被调用.我也试过 InRequestScope,但也没有用.

But the Dispose method never gets called. I also tried InRequestScope, but it didn't work either.

到目前为止,唯一真正起作用的是将我需要的对象的实例化包装在每个控制器的 Action 中的 using 语句中.

So far the only thing that has actually worked is wrapping the instantiation of the object that I need in a using statement in EVERY Controller's Action.

像这样:

using (var uow = UnitOfWorkFactory.Create())
{
  var repos = new UserRepository(uow);

  uow.SaveChanges();
}

Ninject 包添加的 NinjectWebCommon 类

 public static class NinjectWebCommon
    {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start()
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            bootstrapper.Initialize(CreateKernel);
        }

        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            bootstrapper.ShutDown();
        }

        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            try
            {
                kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
                kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

                RegisterServices(kernel);
                return kernel;
            }
            catch
            {
                kernel.Dispose();
                throw;
            }
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<IUnitOfWork>().To<SqlUnitOfWork>().InRequestScope();
        }
    }

从上面的代码可以看出,现在我使用的是 kernel.Bind().To().InRequestScope();,但是 我的类的 Dispose 方法仍然没有被调用.

As you can see from the code above, now I'm using kernel.Bind<IUnitOfWork>().To<SqlUnitOfWork>().InRequestScope();, but the Dispose method of my class still never gets called.

是否真的有任何方法可以使用 Ninject 实现与使用 using 语句相同的行为,即当创建的对象被使用时(using 中的最后一行代码>Controller的Action被处理)那个对象的Dispose方法被调用了?

Is there really any way to achieve with Ninject the same behaviour we get with a using statement, that is, when the object created is done being used (the last line of code in the Controller's Action is processed) the Dispose method of that object is called?

推荐答案

Ninject 中有一个 Module 需要安装、配置(和最新)[https://github.com/ninject/ninject/issues/132#issuecomment-42459686] 以便在每个请求结束时确定性地完成此操作(与由 GC 驱动相比).

There's a Module within Ninject that needs to be installed, configured (and up to date)[https://github.com/ninject/ninject/issues/132#issuecomment-42459686] for this to be done deterministically at the end of each request (vs driven by GC).

这篇关于如何配置 Ninject 以便它为每个控制器创建一个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆