Disposing of objects resolved by ChildContainer

Feb 11, 2011 at 4:37 PM

It seems from my testing that the only way I can get Unity to dispose of objects created with a child container are to (1) make sure that they are registered using the child container rather than the parent, and (2) set the lifetime to ContainerControlledLifetimeManager. When I do this, then when the child container is disposed, all objects created by it are also disposed. Is this correct?

Is there any way to register all components with the parent container, but because I am resolving with the child container, once the child container is disposed, to automatically dispose of all created objects?

Feb 12, 2011 at 3:17 AM

Register in the parent using the HierarchicalLifetimeManager. Child containers will keep their own copies of resolved objects, and when the child container is disposed objects it is tracking will be disposed as well.

This will only dispose objects resolved in the child container that was disposed, not in any other container.