【发布时间】:2013-12-23 06:50:13
【问题描述】:
我有一个要求,我必须在我的应用程序中支持两种类型的容器。因此,我创建了一个自定义 ServiceLocator,它为 2 种类型的容器提供服务,即
SimpleIoc (Galasoft)
MefServiceLocatorAdapter
因此,我创建了一个通用服务定位器类,即
public class CommonServiceLocator : ServiceLocatorImplBase
{
private IServiceLocator _iocContainer, _mefContainer;
public CommonServiceLocator(IServiceLocator iocLocator, IServiceLocator mefLocator)
{
_iocContainer = iocLocator;
_mefContainer = mefLocator;
}
protected override IEnumerable<object> DoGetAllInstances(Type serviceType)
{
IEnumerable<object> services;
try
{
services = _iocContainer.GetAllInstances(serviceType);
}
catch (Exception)
{
//Current assumption is that if IocContainer doesn't contain an instance then
//it should look for MefContainer for values
services = _mefContainer.GetAllInstances(serviceType);
}
return services;
}
protected override object DoGetInstance(Type serviceType, string key)
{
object service;
try
{
service = _iocContainer.GetInstance(serviceType, key);
}
catch (Exception)
{
//Current assumption is that if IocContainer doesn't contain an instance then
//it should look for MefContainer for values
service = _mefContainer.GetInstance(serviceType, key); //<== This fails to get an instance
}
return service;
}
public override object GetInstance(Type serviceType, string key)
{
return DoGetInstance(serviceType, key);
}
public override object GetInstance(Type serviceType)
{
return GetInstance(serviceType, null);
}
}
现在,问题是当我尝试使用 GetInstance() 方法通过 IOC 容器获取类实例时,它工作正常。但是,通过 MefContainer 获取类实例会引发一些错误(“System.Core.dll 中发生了“System.InvalidOperationException”类型的第一次机会异常”)。我创建了一个测试类来测试这个场景:
public class ServiceLocatorTest
{
public void CommonServiceLocatorInstanceTest()
{
var serviceLocator = new SimpleIoc();
serviceLocator.Register<GalaSoftIocData>();
AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(typeof(MefCompositionAddin).Assembly));
CompositionContainer compContainer = new CompositionContainer(catalog);
CompositionBatch batch = new CompositionBatch();
batch.AddPart(AttributedModelServices.CreatePart(new MefCompositionAddin()));
compContainer.Compose(batch);
var vl = compContainer.GetExportedValue<MefCompositionAddin>(); // <== This gives instance correctly
var mefserviceLocator = new MefServiceLocatorAdapter(compContainer);
var commonServiceLocator = new CommonServiceLocator(serviceLocator, mefserviceLocator);
var galaSoftData = commonServiceLocator.GetInstance(typeof(GalaSoftIocData));
var mefData = commonServiceLocator.GetInstance(typeof(MefCompositionAddin));
}
}
[Export]
class MefCompositionAddin
{
public string MyData { get { return "Mef's Data composed"; } }
[Import]
public MefCompositionAddin MyObj { get; set; }
}
class MefCompositionData
{
[Import]
public MefCompositionAddin MyAddin { get; set; }
}
class GalaSoftIocData
{
public string MyData { get { return "Galasoft's Data composed"; } }
}
如果我错过了什么,请告诉我。以及可能的原因是什么。提前感谢大家的帮助。
【问题讨论】:
-
您能否提供有关此异常的更多详细信息,例如:堆栈跟踪(如果它很长,则几个顶级调用就足够了)?它有内部异常吗?
-
System.InvalidOperationException 发生 HResult=-2146233079 消息=序列包含多个元素 Source=System.Core StackTrace:在 Microsoft 的 System.Linq.Enumerable.Single[TSource](IEnumerable`1 源) .Practices.Prism.MefExtensions.MefServiceLocatorAdapter.DoGetInstance(Type serviceType, String key) 在 c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft 中的 Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key)。 Practices.ServiceLocation\ServiceLocatorImplBase.cs:第 49 行 InnerException:
-
另外,没有InnerException