我的应用程序有10个WCFService(平台.NET Framework 3.5上的WCFService应用程序),具有相同的软件和硬件,但只有1个例外:
当用户登录时调用此方法:
public IService Select(SelectServiceRequest request)
{
IAxxxService anagServ = IoC.Container.Resolve<IAxxxService>(request.GetRegisteredService().ToString());
return xxxServ;
}
使用GetRegisteredService()实现,ClientIdentifier = 0表示首次执行
public RegisteredServices GetRegisteredService()
{
RegisteredServices res = RegisteredServices.Estxxx;
if (ClientIdentifier == 0)
{
res = RegisteredServices.Anaxxx;
}
else if (ClientIdentifier == 1)
{
res = RegisteredServices.Prixxx;
}
else if (ClientIdentifier == 2)
{
res = RegisteredServices.Estrxxx;
}
else if (ClientIdentifier == 3)
{
res = RegisteredServices.LixxAnagrxx;
}
return res;
}
使用IOC代码实现和启动:
internal class IoC{
private static IUnityContainer container = new UnityContainer();
private static bool isInitialized = false;
public static IUnityContainer Container
{
get
{
if (!isInitialized)
{
lock (container)
{
if (!isInitialized)
container.RegisterType<IService, EstxxService>(RegisteredServices.Esxxxxx.ToString());
container.RegisterType<IService, StaxxxService>(RegisteredServices.Anaxxxx.ToString());
container.RegisterType<IService, PrixxxService>(RegisteredServices.Prixxxx.ToString());
container.RegisterType<IService, LixxxAxxxService>(RegisteredServices.LixxxAnagxx.ToString());
isInitialized = true;
}
}
}
return container;
}
}
}
我在执行方法“Resolve”上有这个例外:
System.Reflection.TargetInvocationException:异常已经发生 由调用目标抛出。 ---&GT; Microsoft.Practices.Unity.ResolutionFailedException:依赖项的解析失败,type = “ApCon.IService”,name =“Anagxxx”。异常消息是: 当前的构建操作(构建密钥Build Key [ApCon.StandardService,Anaxxxx])失败: 当前的构建操作(构建密钥构建密钥[ApCon.StandardService,Anaxxxx])失败:索引位于 数组的边界。 (策略类型DynamicMethodConstructorStrategy,索引0)(策略类型BuildPlanStrategy,索引3) ---&GT; Microsoft.Practices.ObjectBuilder2.BuildFailedException: 当前的构建操作(构建密钥构建密钥[ApCon.StandardService,Anagrafe])失败: 当前的构建操作(构建密钥构建密钥[ApCon.StandardService,Anagrafe])失败:索引位于 数组的边界。 (策略类型DynamicMethodConstructorStrategy,索引0)(策略类型BuildPlanStrategy,索引3) ---&GT; Microsoft.Practices.ObjectBuilder2.BuildFailedException:当前的构建操作 (构建密钥构建密钥[ApCon.StandardService,Anagxxx])失败: 指数数组的边界之外。 (策略类型DynamicMethodConstructorStrategy,索引0) ---&GT; System.IndexOutOfRangeException:索引超出了数组的范围。在 System.Collections.Generic.List1.Add(T item)at Microsoft.Practices.ObjectBuilder2.DependencyResolverTrackerPolicy.AddResolverKey(对象 关键)at Microsoft.Practices.ObjectBuilder2.ConstructorSelectorPolicyBase`1.CreateSelectedConstructor(IBuilderContext context,ConstructorInfo ctor)at Microsoft.Practices.ObjectBuilder2.ConstructorSelectorPolicyBase1.SelectConstructor(IBuilderContext 上下文) Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.PreBuildUp(IBuilderContext 上下文) Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)---内部异常堆栈跟踪的结束---在
可能是锁定指令吗?似乎没有注册类型,并且isInitialized变为真实
答案 0 :(得分:0)
我可以告诉您,您正在注册IAnagrafeService
的命名实例,但尝试解析IService
的命名实例。
我的猜测是第一个来自第二个,所以这样的东西应该有效:
string name = request.GetRegisteredService().ToString();
IService anagServ = IoC.Container.Resolve<IAnagrafeService>(name);
(或者你可以改变Unity注册。)
答案 1 :(得分:0)
我认为这与未注册的服务无关。这通常会抛出这个例外:
当前类型IFoo是一个接口,无法构造。是 你错过了类型映射吗?
这可能是锁定的结果。您可以尝试使用此模式:
public static class IocContainer
{
private static readonly Lazy<IUnityContainer> Container = new Lazy<IUnityContainer>(() =>
{
var container = new UnityContainer();
// Do your registrations.
container.RegisterType<IService, EstattoriService>(RegisteredServices.Estrattori.ToString());
container.RegisterType<IService, StandardService>(RegisteredServices.Anagrafica.ToString());
container.RegisterType<IService, PrivacyService>(RegisteredServices.Privacy.ToString());
container.RegisterType<IService, ListAnagService>(RegisteredServices.ListaAnagrafica.ToString());
return container;
});
public static IUnityContainer Instance
{
get { return Container.Value; }
}
}
如果这不是一个选项,您可以尝试不锁定容器本身:
internal class IoC{
private static IUnityContainer container = new UnityContainer();
private static bool isInitialized = false;
private static readonly object padlock = new object(); // lock object.
public static IUnityContainer Container
{
get
{
if (!isInitialized)
{
lock (padlock) // Lock on padlock instead.
{
if (!isInitialized)
container.RegisterType<IService, EstattoriService>(RegisteredServices.Estrattori.ToString());
container.RegisterType<IService, StandardService>(RegisteredServices.Anagrafica.ToString());
container.RegisterType<IService, PrivacyService>(RegisteredServices.Privacy.ToString());
container.RegisterType<IService, ListAnagService>(RegisteredServices.ListaAnagrafica.ToString());
isInitialized = true;
}
}
}
return container;
}
}
我发现这是单身人士模式的一个很好的来源。