Error while disposing of IActivationBlock and importing IKernel

The problem started when I tried to use the solution below to use Ninject 3 with the MVC 4 RC Web Api project:

http://www.peterprovost.org/blog/2012/06/19/adding-ninject-to-web-api/

This solution uses IActivationBlock (created using the BeginBlock method from IKernel) to implement the call area. Ninject seems to work fine with a regular project, but when the project uses the Ninject.Extensions.Interception.DynamicProxy extension, the following exception occurs when the Dispose method of the activation block is called:

Error loading Ninject IAdviceRegistry component

Such a component is not registered in the kernel component container.

And the next time a new ActivationBlock is created and the Resolve method is called, the following exception occurs:

Error loading Ninject ICache component

Such a component is not registered in the kernel component container.

It seems that the problem is not directly related to the MVC update, but some kind of incompatibility between DynamicProxy and IActivationBlock.

Edit:

The source of the problem is the fact that one of the types requires IKernel for the constructor and is not directly related to DynamicProxy, but the first exception occurs only when referencing this assembly.

So, the second error (related to ICache) always occurs if your type requires IKernel.

+6
source share
1 answer

To make your analysis pleasant, you create an instance of the class inside ActivationBlock, which directly depends on IKernel.

This is a logical flaw, since the idea of ​​ActivationBlock is to “restore the state” of the kernel after the block has been deleted, and an instance that has access to the kernel and can non-stop change any bindings. (yes, the instance may be IDisposable, which is cleared after itself, then there is no logical flaw - just an unusual use case).

My experience is that the vast majority of these applications are a call to IKernel.Get <...> (...) and friends. Obviously, this is really inside the ActivationBlock: you are simply asking for more than you need: IKernel instead of IResolutionRoot (for example, you don't need IBindingRoot IKernel). Change the types in your class and everything will be fine.

PS Thank you for your analysis for the source of the exception. It helped me with my own problem.

0
source

Source: https://habr.com/ru/post/919070/


All Articles