0

我正在尝试自行覆盖实现 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?

4

1 回答 1

0

我认为您需要在添加新的 MefModuleInitializer 后调用基本方法

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

    base.ConfigureAggregateCatalog();
}
于 2013-04-26T01:12:40.110 回答