Ninject and ChildKernel in MVC3 project: “Error loading Ninject component ICache”

て烟熏妆下的殇ゞ 提交于 2019-12-14 03:12:15

问题


I'm using Ninject (3.0) in my ASP.Net MVC3 project. At some point of request handling I want to execute some tasks.

I'm using session-per-request pattern but don't want these tasks to share the same Session as current Request has. So, I thought, ChildKernels could help me with this issue. I was going to create another binding for ISession in child kernel, but started with that:

var child = new Ninject.Extensions.ChildKernel.ChildKernel(NinjectMVC3.Kernel);
child.Dispose();

If I execute the code like that during request I get Error loading Ninject component ICache exception at the end of my request (not at .Dispose() call). If I remove child.Dispose() everything is fine.

So, am I choosing the right way to go with ChildKernel? Is it safe to use the child kernels without explicitly disposing them? Why Ninject throws at the request end if I dispose the child kernel?


回答1:


The exception is thrown because you load all the modules from the extensions into the child kernel. Creating the kernel with LoadExtensions=false would fix that problem. But this is not the solution in your case.

The intention of the ChildKernel isn't really for different scoping. You would need to register everything for the tasks on that child kernel. Just the session won't be enough. Adding just a conditional binding for the session is far the better option.



来源:https://stackoverflow.com/questions/10958445/ninject-and-childkernel-in-mvc3-project-error-loading-ninject-component-icache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!