Falling into the deep hole of Constructor Autodiscovery
I am using Unity right now to do some IoC/Dependency Injection. All I wanted to get is an instance of the WCF Client that was auto-generated for me. So naively I thought this would be enough:
- m_container.RegisterType<IService, ServiceClient>();
But that greeted me with a kinda weird error message:
Microsoft.Practices.ObjectBuilder2.BuildFailedException: The current build operation (build key Build Key[Test.ServiceReference.ServiceClient, null]) failed: The current build operation (build key Build Key[Test.ServiceReference.ServiceClient, null]) failed: The type ServiceClient has multiple constructors of length 2. Unable to disambiguate. (Strategy type DynamicMethodConstructorStrategy, index 0) (Strategy type BuildPlanStrategy, index 3) ---> Microsoft.Practices.ObjectBuilder2.BuildFailedException: The current build operation (build key Build Key[Test.ServiceReference.ServiceClient, null]) failed: The type ServiceClient has multiple constructors of length 2. Unable to disambiguate. (Strategy type DynamicMethodConstructorStrategy, index 0) ---> System.InvalidOperationException: The type ServiceClient has multiple constructors of length 2. Unable to disambiguate.
bei Microsoft.Practices.ObjectBuilder2.ConstructorSelectorPolicyBase`1.FindLongestConstructor(Type typeToConstruct)
bei Microsoft.Practices.ObjectBuilder2.ConstructorSelectorPolicyBase`1.SelectConstructor(IBuilderContext context)
bei Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.PreBuildUp(IBuilderContext context)
bei Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
--- Ende der internen Ausnahmestapelüberwachung ---
bei Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
bei Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlanCreatorPolicy.CreatePlan(IBuilderContext context, Object buildKey)
bei Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)
bei Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
--- Ende der internen Ausnahmestapelüberwachung ---
bei Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
bei Microsoft.Practices.Unity.ObjectBuilder.NamedTypeDependencyResolverPolicy.Resolve(IBuilderContext context)
bei BuildUp_Test.ServiceReference.ServiceClient(IBuilderContext )
Turns out: I am used to Windsor Castle... Apparently Unity thinks of itself high enough to decide what it can and can't construct. Meaning - instead of using the constructor it could satisfy with all the types I registered (in other words the default constructor since I did not register anything else) it went straight for the longest constructor it could find - and got rightfully confused since there are multiple in any service client of length two.
Well, after a bit of soul searching I came up with this solution: "Specify the type of constructor I want Unity to use"
- m_container.RegisterType<IService, ServiceClient>()
- .Configure<InjectedMembers>()
Now the default constructor is used when resolving IService to ServiceClient. (Note that the constructor of InjectionConstructor has no parameters...)
Still think that the Constructor discovery process of Unity is straight brain-dead. But now it works...

“Still think that the Constructor discovery process of Unity is straight brain-dead. But now it works”
agreed.
thanks for the blog post.
Comment on August 18, 2009 @ 19:53:54
Is there any way to achieve the same but using the Unity Configuration from the web config… I haven’t been able to figure it out.
Thanks.
Comment on September 25, 2009 @ 22:29:19
You might want to try this from the web.config:
< type type=”IService” mapTo=”ServiceClient” >
< lifetime type=”singleton” / >
< typeConfig extensionType=”Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration” >
< constructor / >
< / typeConfig >
< / type >
Comment on January 22, 2010 @ 19:10:52