简体   繁体   English

将 RegisterBootstrapperProvidedTypes 与 PRISM MEF 一起使用时出现 ImportCardinalityMismatchException

[英]ImportCardinalityMismatchException when using RegisterBootstrapperProvidedTypes with PRISM MEF

when I override RegisterBootstrapperProvidedTypes and I try to register my own WCServiceAgent the Bootstrapper throws a当我覆盖 RegisterBootstrapperProvidedTypes 并尝试注册我自己的 WCServiceAgent 时,Bootstrapper 会抛出一个

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

The exception occurs with:异常发生在:

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

and also with还有

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

in my Bootstrapper class在我的 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();
    }
}

How can I fix this?我怎样才能解决这个问题? Whats the cause of the exception?异常的原因是什么?

You need to call the base implementation first.您需要先调用基本实现。

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

    // your custom registrations go here...        
}

If you take a look at the implementation of the bootstrapper base class, you'll see that there are 4 exports which need to be composed:如果您查看引导程序基类的实现,您会发现有 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM