将 RegisterBootstrapperProvidedTypes 与 PRISM MEF 结合使用时出现 ImportCardinalityMismatchException
ImportCardinalityMismatchException when using RegisterBootstrapperProvidedTypes with PRISM MEF
当我重写 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));
}
在我的引导程序中 class
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...
}
如果您查看引导程序基础 class 的 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);
当我重写 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));
}
在我的引导程序中 class
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...
}
如果您查看引导程序基础 class 的 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);