Reputation: 1
My application have a 10 WCFService ( WCFService Application on platform .NET Framework 3.5) with same software and hardware but only 1 takes this exception:
When user is logged in invoke this method:
public IService Select(SelectServiceRequest request)
{
IAxxxService anagServ = IoC.Container.Resolve<IAxxxService>(request.GetRegisteredService().ToString());
return xxxServ;
}
with GetRegisteredService() implementation with ClientIdentifier = 0 for first execution
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;
}
with IOC code implementation and Initilization:
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;
}
}
}
i have this exception on execution method "Resolve":
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = "ApCon.IService", name = "Anagxxx". Exception message is: The current build operation (build key Build Key[ApCon.StandardService, Anaxxxx]) failed: The current build operation (build key Build Key[ApCon.StandardService, Anaxxxx]) failed:Index was outside the bounds of the array. (Strategy type DynamicMethodConstructorStrategy, index 0) (Strategy type BuildPlanStrategy, index 3) ---> Microsoft.Practices.ObjectBuilder2.BuildFailedException: The current build operation (build key Build Key[ApCon.StandardService, Anagrafe]) failed: The current build operation (build key Build Key[ApCon.StandardService, Anagrafe]) failed: Index was outside the bounds of the array. (Strategy type DynamicMethodConstructorStrategy, index 0) (Strategy type BuildPlanStrategy, index 3) ---> Microsoft.Practices.ObjectBuilder2.BuildFailedException: The current build operation (build key Build Key[ApCon.StandardService, Anagxxx]) failed: Index was outside the bounds of the array. (Strategy type DynamicMethodConstructorStrategy, index 0) ---> System.IndexOutOfRangeException: Index was outside the bounds of the array. at System.Collections.Generic.List1.Add(T item) at Microsoft.Practices.ObjectBuilder2.DependencyResolverTrackerPolicy.AddResolverKey(Object key) at Microsoft.Practices.ObjectBuilder2.ConstructorSelectorPolicyBase`1.CreateSelectedConstructor(IBuilderContext context, ConstructorInfo ctor) at Microsoft.Practices.ObjectBuilder2.ConstructorSelectorPolicyBase1.SelectConstructor(IBuilderContext context) at Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.PreBuildUp(IBuilderContext context) at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) --- End of inner exception stack trace --- at
Could it be the lock instruction? It seems that types weren't registered and isInitialized becomed true
Upvotes: 0
Views: 8541
Reputation: 10851
I don't think it's related to the services not being registered. That usually throws this Exception:
The current type, IFoo, is an interface and cannot be constructed. Are you missing a type mapping?
It can be a result of the lock. You can try this pattern instead:
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; }
}
}
If that's not an option, you can try to not lock the container itself:
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;
}
}
I find this a good source for the singleton pattern.
http://csharpindepth.com/Articles/General/Singleton.aspx
Upvotes: 0
Reputation: 5498
Near as I can tell, you're registering named instances of IAnagrafeService
, but trying to resolve a named instance of IService
.
My guess would be that the first derives from the second, so something like this should work:
string name = request.GetRegisteredService().ToString();
IService anagServ = IoC.Container.Resolve<IAnagrafeService>(name);
(Or you could change the Unity registration, of course.)
Upvotes: 0