Loading Modules in MEF

Apr 20, 2015 at 4:56 PM
How does the "ConfigureAggregateCatalog" differ from "ConfigureModuleCatalog"? The prism 5.0 quickstart example on mef shows 3 ways of loading modules: from assembly catalog, from directory catalog, and from a config file. Looks like the AggregateCatalog.Catalogs.Add(new AssemblyCatalog(...)) is enough to find and load modules. Without the ConfigureModuleCatalog's ModuleCatalog.Add(new ModuleInfo(...) I am not even able to get any error; the module is not loaded.

I get the following exception when loading the module in the way shown below.

An unhandled exception of type 'Microsoft.Practices.Prism.Modularity.ModuleTypeLoadingException' occurred in Microsoft.Practices.Prism.Composition.dll

Additional information: Failed to load type for module ExportBuildPreferencesModule.

Error was: Unable to locate the module with type 'FourZero.ExportBuild.Preferences.ExportBuildPreferencesModule, FourZero.ExportBuild.Preferences, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' among the exported modules. Make sure the module name in the module catalog matches that specified on ModuleExportAttribute for the module type..` If I remove the ConfigureModuleCatalog method, I get: Additional information: Module ExportBuildPreferencesModule was not found in the catalog.

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

protected override void InitializeModules()
  {
      this.ModuleCatalog.Initialize();
  }

protected override void InitializeShell()
{
    base.InitializeShell();
    Application.Current.MainWindow = (Window)this.Shell;
    Application.Current.MainWindow.Show();
    this.moduleManager = this.Container.GetExportedValue<IModuleManager>();
    this.moduleManager.LoadModule("ExportBuildPreferencesModule");
    //this.moduleManager;
}

private IModuleManager moduleManager;

protected override void ConfigureAggregateCatalog()
{
    base.ConfigureAggregateCatalog();
    //get the exported Shell from the assembly containing the bootstrapper (this) (MainWindow)
    this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(BootStrapper).Assembly));


    //get the exports from the Preferences assembly
    this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ExportBuildPreferencesModule).Assembly));
}


protected override void ConfigureModuleCatalog()
{

     base.ConfigureModuleCatalog();
     string path = @"G:\ExportBuild\FourZero.ExportBuild\FourZero.ExportBuild.Main\bin\Debug\FourZero.ExportBuild.Preferences.dll";
     Type modulePreferencesType = typeof (ExportBuildPreferencesModule);
     this.ModuleCatalog.AddModule(new ModuleInfo()
                                 {
                                     ModuleName   = modulePreferencesType.Name,
                                     ModuleType = modulePreferencesType.AssemblyQualifiedName,
                                     Ref = new Uri(path, UriKind.RelativeOrAbsolute).AbsoluteUri,
                                     InitializationMode = InitializationMode.WhenAvailable

                                 });            
}
}
Following is the class that exports the module. Changing to [ModuleExport("ExportBuildPreferenceModule", typeof(ExportBuildPreferencesModule))] gives the same error.

[ModuleExport( typeof(ExportBuildPreferencesModule))]
public class ExportBuildPreferencesModule : IModule
{
//[Import(AllowRecomposition = false)] 
private readonly IRegionManager _regionManager;

[ImportingConstructor]
public ExportBuildPreferencesModule(IRegionManager inRegionManager)
{
    this._regionManager = inRegionManager;
}

[Import]
private ExportBuildPreferencesView _exportBuildPreferencesView;
public void Initialize()
{
     this._regionManager.RegisterViewWithRegion(RegionNames.ExportBuildPreferencesRegion,
     typeof (ExportBuildPreferencesView));
}
}