Error when disposing a IActivationBlock and importing IKernel
Asked Answered
R

1

6

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

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

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

Error loading Ninject component IAdviceRegistry

No such component has been registered in the kernel's component container.

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

Error loading Ninject component ICache

No such component has been registered in the kernel's component container.

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

Edit:

The source of the problem is when one of the types requires IKernel on the constructor, and it's not directly related to DynamicProxy, but the first exception only occurs when you references this assembly.

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

Reservation answered 26/6, 2012 at 20:25 Comment(1)
Anyone ever figure out a workarround?Marocain
S
0

To summerize your nice analysis: you create an instance of a class inside an ActivationBlock which has dependancy on the IKernel directly.

This is a logical flaw, as the idea of an ActivationBlock is to "restore the state" of the kernel after the block is disposed and an instance that has access to the kernel and can change unrevertably any of the bindings. (yes, the instance could be an IDisposable which cleans up after its self; then there is no logical flaw -- just an unusual use case).

My experiance is that the vast majority of these uses are to call IKernel.Get<...>(...) and friends. Obviously this is valid inside an ActivationBlock: you are just requesting more than you need: an IKernel instead of IResolutionRoot (e.g. you don't need IBindingRoot of IKernel). Change the types in your class and you will be fine.

P.S. Thanks for your analysis for the source of the exception. It helped me with my own problem.

Sordello answered 7/11, 2014 at 18:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.