1

Closed

Unity Integration: Order of registrations and ChildContainers

description

Hey,
 
We are currently using UnityIntgration in a Silverlight 4 / .net 4 beta2 project and we have encountered several bugs :
The first one, wich is least critical is that if you register types in your container before calling RegisterCatalog, MEF cannot resolve these registered types.
The second, which if very critical (as complex UI architecture depends highly on it), is that MEF components do not respect ChildContainer Registered Instances.
 
Here is an exemple of code that fails :
 
        IUnityContainer container = ConfigureMefThenUnity();
 
        var unityComponent = container.Resolve<IUnityComponent>();
        container = container.CreateChildContainer();
        container.RegisterInstance<IUnityComponent>(unityComponent);
 
        var component = container.Resolve<IMefDependingOnUnity2>();
        Assert.IsNotNull(component);
        Assert.IsNotNull(component.UnityComponent);
        Assert.AreEqual(unityComponent, component.UnityComponent); // fails here, MEF is using the rule defined on the parent container instead of the overriden one in the child container
 
Attached is the full test file highlighting the 2 bugs described above

file attachments

Closed Dec 28, 2009 at 12:20 PM by pwlodek

comments

pwlodek wrote Dec 28, 2009 at 12:19 PM

Bug 1
Unity does not provide tracking mechanism that is available to the developer. In order to make types registered in the Unity available to MEF before calling RegisterCatalog, developer needs to explicitly add tracking extension to the Unity. This is done in single line of code, ofcourse MEF does not participate here.

var container = new UnityContainer();
TypeRegistrationTrackerExtension.RegisterIfMissing(container);

Bug 2
In order to make a child container properly work with MEF, developer needs to manually enable MEF integration.

IUnityContainer container = new UnityContainer();

container = container.CreateChildContainer();
container.EnableCompositionIntegration();

pwlodek wrote Dec 28, 2009 at 12:19 PM

Bug 1
Unity does not provide tracking mechanism that is available to the developer. In order to make types registered in the Unity available to MEF before calling RegisterCatalog, developer needs to explicitly add tracking extension to the Unity. This is done in single line of code, ofcourse MEF does not participate here.

var container = new UnityContainer();
TypeRegistrationTrackerExtension.RegisterIfMissing(container);

Bug 2
In order to make a child container properly work with MEF, developer needs to manually enable MEF integration.

IUnityContainer container = new UnityContainer();

container = container.CreateChildContainer();
container.EnableCompositionIntegration();

wrote Dec 28, 2009 at 12:20 PM

wrote Feb 14, 2013 at 8:47 PM

wrote May 16, 2013 at 10:39 AM