我正在尝试自行覆盖实现 Mefmoduleinitializer,如 使用 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);
}
}
}
在引导程序中:
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?