Rediscovering the Obvious

…stumbling in the footsteps of greatness

Forgetting yourself in MEF w/ Prism

without comments

Just a quick note in case anybody else runs into this issue in Prism for WPF.

In the code below, the line “return Container.GetExport<Shell>().Value” was failing and telling me with the following message:

System.ComponentModel.Composition.ChangeRejectedException was unhandled Message=The composition remains unchanged. The changes were rejected because of the following error(s): The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.

1) No valid exports were found that match the constraint

‘((exportDefinition.ContractName == “ABC.DEF.MenuViewModel”)

AndAlso (exportDefinition.Metadata.ContainsKey(“ExportTypeIdentity”)

AndAlso “ABC.DEF.MenuViewModel”.Equals(exportDefinition.Metadata.get_Item(“ExportTypeIdentity”))))’

, invalid exports may have been rejected.

Resulting in: Cannot set import ‘ABC.DEF.Shell.ViewModel (ContractName=”ABC.DEF.MenuViewModel”)’ on part ‘ABC.DEF.Shell’.

Element: ABC.DEF.Shell.ViewModel (ContractName=”ABC.DEF.MenuViewModel”) –>  ABC.DEF.Shell

It turns out, this was for a very simple reason: I’d failed to actually include the line that sets the current assembly as one of the assemblies in the AggregateCatalog. Don’t do that!

    internal class Bootstrapper : MefBootstrapper
    {
        protected override DependencyObject CreateShell()
        {
            return Container.GetExport< Shell >().Value;
        }
        protected override void InitializeShell()
        {
            base.InitializeShell();
            App.Current.MainWindow = (Window)this.Shell;
        }
        protected override void ConfigureAggregateCatalog()
        {
            base.ConfigureAggregateCatalog();
            AggregateCatalog.Catalogs.Add(new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly()));
            AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ABC.DEF.Module.ContactModule).Assembly));
        }
    }

Written by erwilleke

December 10th, 2010 at 12:49 am

Posted in Uncategorized

Leave a Reply