Problem with infinite loop when container with child container has been injected.

Topics: Enterprise Library Core, General discussion, Pre-release discussions
Oct 7, 2008 at 7:03 AM
How to inject whole container including the child container without infinite loop? 

When I inject a parent container with the child container, only the parent container is injected. To inject the whole container including the child container, the child container must also be injected. This is where the problem occured: when you inject a child containercreated using 'CreateChildContainer', it will results in a infinite loop when the child container is to be disposed as the example shown below:

    //Infinite loop example
this.uc = container.CreateChildContainer();
this.uc.Dispose(); // Infinite loop

That means you can not inject a whole container including the child container, and it is also difficult to use injection of a container in pratice if you don't know for sure if the container you want inject is created as a child container or not.

Any solution for this problem? or will this be fixed in next release of Enterprice Library?

Kind regard,
Oct 7, 2008 at 3:04 PM

Register the instance providing an instance of ExternallyControlledLifetimeManager. The next release of Unity will do this for you automatically.

You'll get better feedback for these Unity-specific questions if you post them to the Unity project's discussion list.

Oct 8, 2008 at 1:05 PM
Dear Fernando,

Thank you for your advice of where I should post my Unity questionaries.

Probably I didn't describe the problem clearly yesterday. 
The problem is when I inject a child UnityContainer using RegisterInstance with the child UnityContainer itself as parameter:
    IUnityContainer container = new UnityContainer();
    IUnityContainer childCOntainer = container.CreateChildContainer();
    // Inject the child container itself:

Then the call of Dispose on the childContainer will results in a infinite loop.

This means, we can not inject a UnityContainer for sure by register the container itself, it may results a infinite loop when the container has to be Disposed. 

Thank you in advance.
Oct 8, 2008 at 6:20 PM
Edited Oct 8, 2008 at 6:30 PM


I'll stick to my original answer: register the container using an ExternallyControlledLifetimeManager. This lifetime manager will not try to dispose the object it manages, so you won't get the unbounded recursion. Here's how you would do it:

childContainer.RegisterInstance<IUnityContainer>(childContainer, new ExternallyControlledLifetimeManager());

Dealing with child containers shouldn't be a factor here.