I recently added both Ninject and Ninject.Web.Mvc to an existing ASP.NET MVC 4 web application. When browsing the site, I receive the following exception, seemingly at random:

[InvalidOperationException: Error loading Ninject component ICache No such component has been registered in the kernel's component container.

Suggestions: 1) If you have created a custom subclass for KernelBase, ensure that you have properly implemented the AddComponents() method. 2) Ensure that you have not removed the component from the container via a call to RemoveAll(). 3) Ensure you have not accidentally created more than one kernel. ] Ninject.Components.ComponentContainer.Get(Type component) in c:\Projects\Ninject\ninject\src\Ninject\Components\ComponentContainer.cs:160 Ninject.Components.ComponentContainer.Get() in c:\Projects\Ninject\ninject\src\Ninject\Components\ComponentContainer.cs:116 Ninject.Web.Common.<>c__DisplayClass2.b__1(IKernel kernel) in c:\Projects\Ninject\Ninject.Web.Common\src\Ninject.Web.Common\OnePerRequestHttpModule.cs:74 Ninject.GlobalKernelRegistration.MapKernels(Action`1 action) in c:\Projects\Ninject\ninject\src\Ninject\GlobalKernelRegistration.cs:75 Ninject.Web.Common.OnePerRequestHttpModule.DeactivateInstancesForCurrentHttpRequest() in c:\Projects\Ninject\Ninject.Web.Common\src\Ninject.Web.Common\OnePerRequestHttpModule.cs:74 Ninject.Web.Common.OnePerRequestHttpModule.b__0(Object o, EventArgs e) in c:\Projects\Ninject\Ninject.Web.Common\src\Ninject.Web.Common\OnePerRequestHttpModule.cs:56 System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69

Once I recieve the exception above, simply refreshing or clicking back seems to cause the site to begin functioning normally again. No one area of the site seems to cause it to occur any more than another.

The issue is quite similar to the question asked here: Ninject + "Error loading Ninject component ICache". However, the issue in that question seems to be caused by a bug with the extension itself, is over a year old, and looks to have been resolved.

This occurs without any modification to the NinjectWebCommon.cs file, whatsoever. (No modules loaded, no bindings added.) The only way for me to get it to stop is to exclude NinjectWebCommon.cs from the project, altogether.

I'm not sure what other information to include, as I've done little other than install the Nuget packages. Perhaps worth noting is that this project does include references to other assemblies that utilize Ninject for DI.

I've looked around quite a bit and am at a loss for why this would occur. Any suggestions would be greatly appreciated.

有帮助吗?

解决方案

Obviously there are multiple Kernels involved in your application (MVC + Libs). Creating and disposing kernel instances during runtime of the application is something that should be avoided. I strongly suggest to change your design so that as few kernels are created at application start. And none is created later (including all libs).

Having said that, I don't see any way in the code how this problem can happen with the latest version of Ninject. It may be worth verifying that the Application and ALL the libraries are on the latest version of Ninject.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top