问题描述
我最近尝试了 Ninject
与 Ninject.Web.Mvc
延期,我已经发现了一些奇特的,虽然没有突破,令人目不暇接。
I have recently tried out Ninject
with the Ninject.Web.Mvc
extension, and I've noticed something peculiar and, while not breaking, confusing.
在 NinjectHttpApplication
抽象类,有一个定义为一个构造函数如下。
In the NinjectHttpApplication
abstract class, there is a constructor defined as follows..
/// <summary>
/// Initializes a new instance of the <see cref="NinjectHttpApplication"/> class.
/// </summary>
protected NinjectHttpApplication()
{
this.onePerRequestModule = new OnePerRequestModule();
this.onePerRequestModule.Init(this);
}
我把一个调试器断点这里,这被叫了几声。我找不到它的任何真正的文档。在实施code,有这条线映入我的眼帘。
I have placed a debugger breakpoint here, and this gets called a few times. I cannot find any real documentation on it. In the implementation code, there is this line that catches my eye.
if (kernel.Settings.Get("ReleaseScopeAtRequestEnd", true))
{
OnePerRequestModule.StartManaging(kernel);
}
我的问题如下...
My questions are as follows...
- 什么是
OnePerRequestModule
- 这是为什么构造函数被调用多次?
- 什么是这样做的目的
StartManaging
方法,如果调用构造函数多次?
- What is
OnePerRequestModule
- Why is this constructor being called multiple times?
- What is the purpose of this
StartManaging
method, if the constructor is called multiple times?
推荐答案
的 OnePerRequestModule
将 InRequestScope()
D从内核的缓存一个对象>每个HTTP请求完成后。
The OnePerRequestModule
removes InRequestScope()
d objects from the Kernel's Cache upon completion of each HTTP Request.
的 NinjectHttpApplication
男星被称为多的时间,因为IIS创建其中几个。其中 NinjectHttpApplication
只能一次处理一个请求。因此,IIS每个线程产生(至少)一个实例。
The NinjectHttpApplication
ctor is called multiple time because IIS creates several of them. One NinjectHttpApplication
can only handle one request at a time. So IIS generates (at least) one instance per thread.
StartManaging
告诉所有的 OnePerRequestModules
,他们将释放 InRequestScoped
从指定的内核对象请求结束后。
StartManaging
tells all OnePerRequestModules
that they shall release the InRequestScoped
objects from the specified Kernel after the Request has Ended.
这篇关于Ninject和OnePerRequestModule的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!