我正试图自己覆盖Mefmoduleinitializer的实现,如Replacing Default Types Using MEF中所述。 我的课(在Shells大会中)是:
[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);
}
}
}
在Bootstrapper中:
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替换为MEF CompositionContainer中的SafeMefModuleInitializer?
答案 0 :(得分:0)
我认为你需要在添加新的MefModuleInitializer后调用基本方法
protected override void ConfigureAggregateCatalog()
{
AggregateCatalog.Catalogs.Add(
new DirectoryCatalog(".")
);
AggregateCatalog.Catalogs.Add(
new DirectoryCatalog(Properties.Settings.Default.ModuleCatalogPath)
);
base.ConfigureAggregateCatalog();
}