Ninject和OnePerRequestModule

Cie*_*iel 12 ninject ninject-extensions asp.net-mvc-3

我最近尝试了NinjectNinject.Web.Mvc扩展,我已经发现了一些奇特的和,而不是突破,令人目不暇接.

NinjectHttpApplication抽象类中,有一个构造函数定义如下..

    /// <summary>
    /// Initializes a new instance of the <see cref="NinjectHttpApplication"/> class.
    /// </summary>
    protected NinjectHttpApplication()
    {
        this.onePerRequestModule = new OnePerRequestModule();
        this.onePerRequestModule.Init(this);
    }
Run Code Online (Sandbox Code Playgroud)

我在这里放置了一个调试器断点,这会被调用几次.我找不到任何真实的文档.在实现代码中,有这条线引起了我的注意.

            if (kernel.Settings.Get("ReleaseScopeAtRequestEnd", true))
            {
                OnePerRequestModule.StartManaging(kernel);
            }
Run Code Online (Sandbox Code Playgroud)

我的问题如下......

  • 什么是 OnePerRequestModule
  • 为什么多次调用此构造函数?
  • StartManaging如果构造函数被多次调用,那么此方法的目的是什么?

Rem*_*oor 13

OnePerRequestModule移除了InRequestScope()d从内核的对象缓存每个HTTP请求完成后.

NinjectHttpApplication构造函数被称为多的时间,因为IIS创建几个人.一次NinjectHttpApplication只能处理一个请求.所以IIS每个线程生成(至少)一个实例.

StartManaging告诉所有OnePerRequestModules他们InRequestScoped在Request已结束后将从指定的内核中释放对象.