gpt4 book ai didi

c# - 将 RegisterBootstrapperProvidedTypes 与 PRISM MEF 结合使用时出现 ImportCardinalityMismatchException

转载 作者:太空宇宙 更新时间:2023-11-03 13:00:44 25 4
gpt4 key购买 nike

当我覆盖 RegisterBootstrapperProvidedTypes 并尝试注册我自己的 WCServiceAgent 时,Bootstrapper 会抛出一个

ImportCardinalityMismatchException
Additional information: No exports were found that match the constraint:
ContractName Microsoft.Practices.ServiceLocation.IServiceLocator
RequiredTypeIdentity Microsoft.Practices.ServiceLocation.IServiceLocator

异常发生在:

 protected override void RegisterBootstrapperProvidedTypes()
{
this.Container.ComposeExportedValue<IModuleCatalog>(this.ModuleCatalog);
this.Container.ComposeExportedValue<AggregateCatalog>(this.AggregateCatalog);
}

还有

    protected override void RegisterBootstrapperProvidedTypes()
{
this.Container.ComposeExportedValue<MyWCFServiceAgent>(new MyWCFServiceAgent(1));
}

在我的 Bootstrapper 类中

public class Bootstrapper : MefBootstrapper
{
//protected override void RegisterBootstrapperProvidedTypes()
//{
//see above code
//}

protected override System.Windows.DependencyObject CreateShell()
{
return this.Container.GetExportedValue<ShellWindow>();
}
protected override void ConfigureAggregateCatalog()
{
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Bootstrapper).Assembly));
base.ConfigureAggregateCatalog();
}
protected override Microsoft.Practices.Prism.Regions.IRegionBehaviorFactory ConfigureDefaultRegionBehaviors()
{
var factory = base.ConfigureDefaultRegionBehaviors();
//factory.AddIfMissing("AutoPopulateExportedViewsBehavior", typeof(AutoPopulateExportedViewsBehavior));
return factory;
}
protected override IModuleCatalog CreateModuleCatalog()
{
return base.CreateModuleCatalog();
}
protected override void InitializeShell()
{
base.InitializeShell();
App.Current.MainWindow = (Window)this.Shell;
App.Current.MainWindow.Show();
}
}

我该如何解决这个问题?异常原因是什么?

最佳答案

您需要先调用基础实现。

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

// your custom registrations go here...
}

如果你看一下 implementation在 Bootstrap 基类中,您会看到需要组合 4 个导出:

this.Container.ComposeExportedValue<ILoggerFacade>(this.Logger);
this.Container.ComposeExportedValue<IModuleCatalog>(this.ModuleCatalog);
this.Container.ComposeExportedValue<IServiceLocator>(new MefServiceLocatorAdapter(this.Container));
this.Container.ComposeExportedValue<AggregateCatalog>(this.AggregateCatalog);

关于c# - 将 RegisterBootstrapperProvidedTypes 与 PRISM MEF 结合使用时出现 ImportCardinalityMismatchException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32369212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com