棱镜ServiceLocator GetInstance和MEF [英] Prism ServiceLocator GetInstance and MEF
问题描述
我正在尝试使用Microsoft.Practices.ServiceLocation.ServiceLocator和MEF.接口IServiceLocator定义带有两个参数的方法GetInstance.第一个参数是serviceType,第二个参数是关键.
I'm trying to use Microsoft.Practices.ServiceLocation.ServiceLocator and MEF. Interface IServiceLocator defines method GetInstance with two parameters. First parameter is serviceType and second paremeter is key.
[Export(typeof(IMyInterface)), PartCreationPolicy(CreationPolicy.Shared)]
public class Class1 : IMyInterface
{}
[Export(typeof(IMyInterface)), PartCreationPolicy(CreationPolicy.Shared)]
public class Class2: IMyInterface
{}
我想通过Prism ServiceLocator GetInstance方法获取Class1的实例:
I want to get instance of Class1 via Prism ServiceLocator GetInstance method:
ServiceLocator.Current.GetInstance(typeof(IMyInterface),"Key");
但是我不知道应该如何以及在哪里定义键".我试图在导出属性中定义键:
But I have no idea how and where should be "key" defined. I tried to define key in export attribute:
[Export("Key1",typeof(IMyInterface)), PartCreationPolicy(CreationPolicy.Shared)]
public class Class1 : IMyInterface
{}
[Export("Key2",typeof(IMyInterface)), PartCreationPolicy(CreationPolicy.Shared)]
public class Class2: IMyInterface
{}
当我使用关键参数调用方法GetInstance时
When I call method GetInstance with key parameter
ServiceLocator.Current.GetInstance(typeof(IMyInterface),"Key1");
我得到Microsoft.Practices.ServiceLocation.ActivationException(尝试获取IMyInterface类型的实例,键为"Key1"的实例时发生激活错误).有人知道如何定义导出密钥吗? 谢谢
I get Microsoft.Practices.ServiceLocation.ActivationException (Activation error occured while trying to get instance of type IMyInterface, key "Key1"). Does anybody know how can be the export key defined? Thanks
推荐答案
Prism使用MefServiceLocatorAdapter
使MEF的CompositionsContainer
适应IServiceLocator
接口.这是它用来获取帮助的实际代码:
Prism uses MefServiceLocatorAdapter
to adapt MEF's CompositionsContainer
to the IServiceLocator
interface. This is the actual code that is used by it to get your part:
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
.但是,我相信您的服务定位器设置不正确,这就是为什么您遇到此异常的原因.如果您使用Prism的MefBootstrapper
初始化应用程序,则应该已经为您完成了.否则,您将需要使用以下代码对其进行初始化:
So as you can see, you're exporting and calling GetInstance
correctly. However, I believe your service locator isn't set up correctly, and that's why you're getting this exception. If you're using Prism's MefBootstrapper
to initialize your application, this should already be done for you. Otherwise, you'll need to use this code to initialize it:
IServiceLocator serviceLocator = new MefServiceLocatorAdapter(myCompositionContainer);
ServiceLocator.SetLocatorProvider(() => serviceLocator);
这篇关于棱镜ServiceLocator GetInstance和MEF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!