MEF can't resolve Shell

Topics: Prism v4 - Silverlight 4
Dec 1, 2010 at 4:25 AM

I am trying to create a simple prism application using MEF, however I am getting the following exception when the application starts up:

System.ComponentModel.Composition.ImportCardinalityMismatchException: No valid exports were found that match the constraint
        '((exportDefinition.ContractName == "SilverlightApplication1.Shell")
        AndAlso (exportDefinition.Metadata.ContainsKey("ExportTypeIdentity")
        AndAlso "SilverlightApplication1.Shell".Equals(exportDefinition.Metadata.get_Item("ExportTypeIdentity"))))',
        invalid exports may have been rejected.
   at System.ComponentModel.Composition.Hosting.ExportProvider.GetExports(ImportDefinition definition, AtomicComposition atomicComposition)
   at System.ComponentModel.Composition.Hosting.ExportProvider.GetExportsCore(Type type, Type metadataViewType, String contractName, ImportCardinality cardinality)
   at System.ComponentModel.Composition.Hosting.ExportProvider.GetExportedValueCore[T](String contractName, ImportCardinality cardinality)
   at System.ComponentModel.Composition.Hosting.ExportProvider.GetExportedValue[T](String contractName)
   at System.ComponentModel.Composition.Hosting.ExportProvider.GetExportedValue[T]()
   at SilverlightApplication1.TestBootstrapper.CreateShell()
   at Microsoft.Practices.Prism.MefExtensions.MefBootstrapper.Run(Boolean runWithDefaultConfiguration)
   at Microsoft.Practices.Prism.Bootstrapper.Run()
   at SilverlightApplication1.App.Application_Startup(Object sender, StartupEventArgs e)
   at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)

What could I be doing wrong? I started with a fresh Silverlight application created by VS 2010. Then added a shell and exported it:

    [Export]
    public partial class Shell : UserControl
    {
        public Shell()
        {
            InitializeComponent();
        }
    }

Changed App.xaml.cs to run the bootstrapper:

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            TestBootstrapper bootstrapper = new TestBootstrapper();
            bootstrapper.Run();
        }

And here's my bootstrapper that creates and initializes the Shell:

    public class TestBootstrapper : MefBootstrapper
    {
        protected override DependencyObject CreateShell()
        {
            return this.Container.GetExportedValue<Shell>();
        }

        protected override void InitializeShell()
        {
            base.InitializeShell();

            Application.Current.RootVisual = (UIElement)this.Shell;
        }
    }

Is there anything that I am omitting? Note that I do not have any modules yet, so I have not bothered with initializing a module catalog.

Thanks.

Naresh

Dec 1, 2010 at 7:05 AM

Answering my own question. Only additional method needed in the TestBootstrapper was:

        protected override void ConfigureAggregateCatalog()
        {
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(TestBootstrapper).Assembly));
        }

I suppose I need to add the bootstrapper's assembly to the catalog even if I don't have any other modules.

Thanks.

Naresh

Developer
Dec 1, 2010 at 5:25 PM
Edited Dec 1, 2010 at 5:25 PM

Hi Naresh,

I am glad that you found a solution for your scenario. Thanks for sharing this with the rest of the community, since other users with a similar situation might find this useful.

Thanks again.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi