How to resolve an instance when still in MefBootStrapper?

Topics: Prism v4 - Silverlight 4
Aug 6, 2011 at 7:33 PM

Hi everyone,

Due some issues I am experiencing I would like to convert Unity to MEF. I have been quite successful so far but got stuck with one case.

How to obtain an instance from the Container when I am still in the MefBootStrapper?

According to this website, I have to point CompositionInitializer's and PRISM's container first together:

Under the covers, the CompositionInitializer is creating its own container and making it available through the CompositionInitializer static interface.  Since Prism also creates it’s own composition container, both Prism and the CompositionInitializer static interfaces need to point to the same container to be able to work together well.   You can setup this connection in Prism bootstrapper you create for your app by calling CompositionHost.Initialize.  This initializes the CompositionHost with the container that Prism uses of instead of creating a new one.  Perhaps the easiest place to do this in the CreateContainer override of your Prism application bootstrapper:



protected override CompositionContainer CreateContainer()
            var container = base.CreateContainer();

            // Initialize the CompositionHost so we can use CompositionInitializer
            return container;


In the shared Infrastructure project I have this:


public class Lookups : ILookups
{ .. }



Within Bootstrapper I have also these:

public ILookups Lookups { get; set; }



And I override my Run like this:


public override void Run(bool runWithDefaultConfiguration)
            #region MEF

            this.Logger = this.CreateLogger();
            this.ModuleCatalog = this.CreateModuleCatalog();
            this.AggregateCatalog = this.CreateAggregateCatalog();
            this.Container = this.CreateContainer();
            Lookups = this.Container.GetExportedValue<ILookups>();
            Lookups.NotifyLookupsLoadedEvent += RunLater;



Lookups = this.Container.GetExportedValue<ILookups>();
crash on me with the error message:

 No valid exports were found that match the constraint '((exportDefinition.ContractName == "SalesContactManagement.Infrastructure.Proxy.ILookups") AndAlso (exportDefinition.Metadata.ContainsKey("ExportTypeIdentity") And
Also "SalesContactManagement.Infrastructure.Proxy.ILookups".Equals(exportDefinition.Metadata.get_Item("ExportTypeIdentity"))))', invalid exports may have been rejected.


What shall I do?  Please advice,

Many Thanks,


Aug 8, 2011 at 10:27 AM


Using MEF at first is not easy.  And I hardly can post the answer here what needed to be done.

The problem is whenever down the hierarchy a type is rejected the whole chain up to the module discovery is affected too. hence the whole module is rejected and it looks like it was not even found.(Misleading)

The only tip I have for people who might have the same problem is to comment everything in Initialize() within the modules and keep them simple.

Run the app and see if it hits those initialze() without breaking.  

Then step by step try adding the first [Import] dependency on the first module. If it breaks, it means that that type has also a sub-dependency that got rejected. Go into that class and see if it imports anything.

If it does, comment it out temporarily and see if the App is runable. If it does, you know where to continue.

Taking these baby steps is the only way making a successful conversion from Unity to MEF.


Good luck,