当我重写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基类的实现,您将看到需要组成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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句