Having trouble using BuildUp method

Oct 4, 2010 at 11:43 PM

I'm trying to call the Buildup method like this

Type type = typeof(T);
Container.BuildUp(type, this);

I've been having so many problems with it that I've completely stripped the class down by removing all the dependent properties and methods.  I would think then that in theory it would just do nothing to the object but it continues to fail every single time.  It seems like it's related to the fact that there are no public constructors on the object but I don't see why that would matter because according to the documentation:

Unity exposes a method named BuildUp that you can use to pass existing object instances through the container in order to apply dependency injection to that object. This is an alternative to resolving the object using any of the other techniques available with Unity. However, remember that the BuildUp method cannot inject dependent objects into constructor parameters, because the object has already been created; it is not created by Unity.

However, the reason I believe the issue is related to the constructor is because I've stepped through Unity's code and this method in DynamicMethodConstructorStrategy.cs is the method throwing the exception (the bold code is returning true)

// Verify the type we're trying to build is actually constructable -
// CLR primitive types like string and int aren't.
private static void GuardTypeIsNonPrimitive(IBuilderContext context, SelectedConstructor selectedConstructor)
    var typeToBuild = context.BuildKey.Type;
        if(typeToBuild == typeof(string) || selectedConstructor == null)  <---- is always true because there are no public constructors on the class
            throw new InvalidOperationException(

Am I doing something completely wrong here?  I don't even understand why the buildup method would be implementing such a function if the object is already instantiated.

Oct 5, 2010 at 6:09 AM

This may be another instance of this already reported bug. Looks like we might have a regression from Unity 1.2 -> 2.0.


Oct 5, 2010 at 12:35 PM

That's a real bummer.  I see the impact is marked 'low', is that simply a function of votes?  This makes it really difficult to work with any third party libraries where I don't have control over the object instantiation.

Oct 5, 2010 at 7:06 PM

We don't really use the impact field, so it never gets set to anything.


Nov 5, 2010 at 10:37 AM
Edited Nov 5, 2010 at 10:40 AM

I have a container per request.  The core for each request is an object space, all my repositories, units of work etc use this object space.  The object space expects two constructors, one for creating a new instance (Single parameter IEcoServiceProvider) and one for recreating an existing instance from the DB (Single parameter IContent).

So I cannot alter the constructors, and nor can I tell Unity to use only one of these constructors.  I hope this breaking change is going to get fixed soon?  I'm having to jump through all kinds of hoops to get my objects' dependencies injected.  There are lots of "TODO: Remove this when Unity bug is fixed" comments throughout my code.



Nov 24, 2010 at 6:05 PM

I'm stuck with the same problem.  I can't really work round this due to the 3rd party library problem.  Does anyone have an idea when this may get fixed?

Nov 24, 2010 at 7:10 PM

An option that has worked for me has been to revert back to version 1.2.  This is not an issue in that version.

Nov 24, 2010 at 7:20 PM
Edited Nov 24, 2010 at 7:20 PM

This is the solution I used...