Unity + C# - "Resolution of the dependency failed"

Dilo picture Dilo · Apr 19, 2016 · Viewed 7.3k times · Source

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

Answer

Petter Hesselberg picture Petter Hesselberg · Apr 19, 2016

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.)