2013-04-22 69 views
0

我试图重写自己的实现Mefmoduleinitializer,如Replacing Default Types Using MEF中所述。 我的类(在壳组装)是:在Prism中使用MEF替换默认IModuleInitializer

[Export(typeof(IModuleInitializer))] 
[PartCreationPolicy(CreationPolicy.Shared)] 
public class SafeMefModuleInitializer: MefModuleInitializer 
{ 
    private readonly IEventAggregator _eventAggregator; 
    private readonly ILoggerFacade _loggerFacade; 

    [ImportingConstructor()] 
    public SafeMefModuleInitializer(IServiceLocator serviceLocator, ILoggerFacade loggerFacade, DownloadedPartCatalogCollection downloadedPartCatalogs, AggregateCatalog aggregateCatalog, IEventAggregator eventAggregator) 
    : base(serviceLocator, loggerFacade, downloadedPartCatalogs, aggregateCatalog) 
    { 
     _eventAggregator = eventAggregator; 
     _loggerFacade = loggerFacade; 
    } 

    public override void HandleModuleInitializationError(ModuleInfo moduleInfo, string assemblyName, Exception exception) 
    { 
     try { 
      base.HandleModuleInitializationError(moduleInfo, assemblyName, exception); 
     } 
     catch (ModuleInitializeException ex) { 
      _loggerFacade.Error(
       string.Format(
        "Module {0} failed to load and has been disabled.", moduleInfo.ModuleName 
       ), 
       ex 
      ); 
      _eventAggregator.GetEvent<ModuleLoadErrorEvent>().Publish(ex); 
     } 
    } 
} 

而且在引导程序:

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

     AggregateCatalog.Catalogs.Add(
      new DirectoryCatalog(".") 
     ); 
     AggregateCatalog.Catalogs.Add(
      new DirectoryCatalog(Properties.Settings.Default.ModuleCatalogPath) 
     ); 
    } 

运行时我得到MEF错误:

"More than one export was found that matches the constraint: ContractName 
Microsoft.Practices.Prism.Modularity.IModuleInitializer RequiredTypeIdentity 
Microsoft.Practices.Prism.Modularity.IModuleInitializer" 

我怎样才能更正取代MefModuleInitializer我SafeMefModuleInitializer在MEF CompositionContainer中?

回答

0

我认为你需要调用基方法添加新MefModuleInitializer

protected override void ConfigureAggregateCatalog() 
{ 
    AggregateCatalog.Catalogs.Add(
     new DirectoryCatalog(".") 
    ); 
    AggregateCatalog.Catalogs.Add(
     new DirectoryCatalog(Properties.Settings.Default.ModuleCatalogPath) 
    ); 

    base.ConfigureAggregateCatalog(); 
}