2013-04-22 50 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(); 
}