Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
179 views
in Technique[技术] by (71.8m points)

c# - How to configure Ninject so that it creates a single instance per Controller

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.

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>();
  }

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

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.

Something like this:

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

  uow.SaveChanges();
}

EDIT:

The NinjectWebCommon class added by the Ninject package

 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();
        }
    }

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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...