BuildUp() fails if there are ambiguous constructors

Nov 3, 2010 at 2:25 PM
Edited Nov 3, 2010 at 2:48 PM

I have some objects for which I cannot alter the constructor signature, there are always two constructors where each has a single parameter

public MyObject(IContent content);
public MyObject(IEcoServiceProvider ecoServiceProvider);

At first I tried using a dependency override to create an instance

UnityContainer.Resolve(objectType, new DependencyOverride(typeof(IContent), content));

I was hoping that unity would use the dependency override as a clue as to which constructor to use, but unfortunately it doesn't :(  I would have preferred to inject my dependencies via the constructor, but anyway...

I then tried the following

var result = constructorInfo.Invoke(new object[] { content });
UnityContainer.BuildUp(objectType, result);

In the past this would work because it looks for DependencyProperty attributes on properties of an existing instance, but for some reason now I get the same exception telling me that there are 2 constructors with 1 parameter.  Why would BuildUp look for a constructor if it is not being asked to construct the instance?

I really need to either create these instances via Unity and to specify which constructor to use (preferable but unlikely I know), or I need to be able to build up an existing instance.

Nov 3, 2010 at 2:29 PM
Edited Nov 3, 2010 at 2:48 PM

If I use the overload of BuildUp which takes just 1 parameter (the existing instance) then my dependency property isn't getting set, and my injection method isn't getting set

//1: This won't work
property IDomainClassValidator DomainClassValidator { get; set; }

//2: Nor will this
public void InsertDependencies(IDomainClassValidator domainClassValidator)
  DomainClassValidator = domainClassValidator; //Never gets called

What's going on here?

Nov 3, 2010 at 4:00 PM

Here is an example app which reproduces the problem...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using System.Reflection;

namespace ConsoleApplication7
public interface IConstructorType1 { }
public interface IConstructorType2 { }
public interface INeedThisDependency { }
public class NeedThisDependency : INeedThisDependency { }

public class MyDomainObject
public MyDomainObject(IConstructorType1 constructorType1) { }
public MyDomainObject(IConstructorType2 constructorType2) { }

public INeedThisDependency Needed { get; set; }

class Program
static void Main(string[] args)
IUnityContainer unityContainer = new UnityContainer();
.RegisterType<INeedThisDependency, NeedThisDependency>();
//Try with type 1 constructor
ConstructorInfo constructorInfo1 = typeof(MyDomainObject).GetConstructor(new Type[] { typeof(IConstructorType1) });
MyDomainObject instance1 = CreateTheInstance(unityContainer, typeof(MyDomainObject), constructorInfo1, null);
//Try with type 2 constructor
ConstructorInfo constructorInfo2 = typeof(MyDomainObject).GetConstructor(new Type[] { typeof(IConstructorType2) });
MyDomainObject instance2 = CreateTheInstance(unityContainer, typeof(MyDomainObject), constructorInfo2, null);

//This is the only point I have any influence over what happens
//So this is the only place I get to change the code.
static MyDomainObject CreateTheInstance(IUnityContainer unityContainer, Type type, ConstructorInfo constructorInfo, object parameters)
var result = (MyDomainObject)constructorInfo.Invoke(new object[] { parameters });
//This will throw an ambiguous constructor exception,
//even though I am not asking it to construct anything
.BuildUp(type, result);
//This will not build up dependencies
if (result.Needed == null)
throw new NullReferenceException("Needed");
return result;


Nov 4, 2010 at 9:34 AM

Does anyone have any insight into this? For me it's a complete show stopper, and as usual the deadline is unreasonably short so it's having a particularly strong negative effect.

Nov 5, 2010 at 1:43 AM

This is a bug reported in this thread.

Your workaround would be to register the MyDomainObject configuring its constructor injection.  If you don't, aside from the exception you encounter when calling BuildUp, any call to the container.Resolve for the MyDomain object will fail since Unity will be unable to decide which constructor to use.

"When a target class contains more than one constructor, Unity will use the one that has the InjectionConstructor attribute applied. If there is more than one constructor, and none carries the InjectionConstructor attribute, Unity will use the constructor with the most parameters. If there is more than one constructor that is the "longest" with the same number of parameters, Unity will raise an exception."

Sarah Urmeneta
Global Technologies & Solutions
Avanade, Inc.