Ninject V2 InRequestScope:在每个请求上触发构造函数

发布于 2024-09-27 04:38:59 字数 382 浏览 0 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牛↙奶布丁 2024-10-04 04:38:59

如果其他人遇到此问题,我的问题与 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文