Ninject V2 InRequestScope:在每个请求上触发构造函数
我在使用 Ninject V2 的 InRequestScope() 来管理工作单元时遇到了困难。
_kernel.Bind<UnitOfWork>().To<SqlUnitOfWork>().InRequestScope();
我希望每当我通过容器请求 UnitOfWork 时,在整个请求过程中始终返回相同的对象。但是,在调试器中,我在对象的构造函数上有一个断点,每当向容器发出请求时,构造函数就会触发,就像正在创建一个新对象一样。
var uow = CreateKernel().Get<UnitOfWork>();
有没有人有过类似的经历,或者我错过了一些重要的事情?
I'm having a hard time with Ninject V2's InRequestScope() for managing a UnitOfWork.
_kernel.Bind<UnitOfWork>().To<SqlUnitOfWork>().InRequestScope();
I expect whenever I request the UnitOfWork via the container that the same object is always returned throughout the request. However, in the debugger I have a breakpoint on the object's constructor and whenever a request is made to the container the constructor fires as though a new object is being created.
var uow = CreateKernel().Get<UnitOfWork>();
Has anyone had similar experiences as this or am I missing something critical?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果其他人遇到此问题,我的问题与 global.asax 文件有关。我手动将 CreateKernal 的结果存储在私有变量中。通过更改为通过 NinjectHttpApplication 基类的 Kernal 属性访问请求创建的内核可以解决该问题。
Incase anyone else has this problem my issue was related to the global.asax file. I was manually storing the result of CreateKernal in a private variable. Changing to access the request's created kernal via the Kernal property of the base NinjectHttpApplication class sorted the issue.