用RegisterBootstrapperProvidedTyp

用RegisterBootstrapperProvidedTyp

当我重写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));
    }


在我的引导程序类中

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...
}


如果您查看bootstrapper基类的implementation,您将看到需要组成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# - 在PRISM MEF中使用RegisterBootstrapperProvidedTypes时,ImportCardinalityMismatchException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32369212/

10-09 08:28