我正在尝试使用 Microsoft.Practices.ServiceLocation.ServiceLocator 和 MEF。接口 IServiceLocator 定义了带有两个参数的方法 GetInstance。第一个参数是 serviceType,第二个参数是 key。
我有两个实现接口 IMyInterface 的类,它们具有 Export 属性:
棱镜用途MefServiceLocatorAdapter
适应 MEFCompositionsContainer
to the IServiceLocator
界面。这是它用来获取您的部分的实际代码:
protected override object DoGetInstance(Type serviceType, string key)
{
IEnumerable<Lazy<object, object>> exports = this.compositionContainer.GetExports(serviceType, null, key);
if ((exports != null) && (exports.Count() > 0))
{
// If there is more than one value, this will throw an InvalidOperationException,
// which will be wrapped by the base class as an ActivationException.
return exports.Single().Value;
}
throw new ActivationException(
this.FormatActivationExceptionMessage(new CompositionException("Export not found"), serviceType, key));
}
如您所见,您正在导出并调用GetInstance
正确。但是,我相信您的服务定位器设置不正确,这就是您收到此异常的原因。如果您使用的是 PrismMefBootstrapper
要初始化您的应用程序,这应该已经为您完成。否则,您需要使用以下代码来初始化它:
IServiceLocator serviceLocator = new MefServiceLocatorAdapter(myCompositionContainer);
ServiceLocator.SetLocatorProvider(() => serviceLocator);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)