Constructor Injection with concrete classes

Nov 21, 2009 at 3:29 PM
Edited Nov 21, 2009 at 8:02 PM

Hi, I'm using Unity 1.2 for a particular web application (C#, .NET 3.5).  In all areas (except one) of my application I'm using interfaces and Unity has been working out the chain beautifully so far. Now in one particular class, the constructor specifies 3 concrete classes all of which are specified in the XML. However Unity seems to ignore this and throws an initialization exception indicating that it is looking for a parameterless constructor. The error stack is pasted below.

System.InvalidOperationException: The type <MyType> does not have a constructor that takes the parameters (). at Microsoft.Practices.Unity.InjectionConstructor.FindConstructor(Type typeToCreate) at Microsoft.Practices.Unity.InjectionConstructor.AddPolicies(Type typeToCreate, String name, IPolicyList policies) at Microsoft.Practices.Unity.InjectedMembers.ConfigureInjectionFor(Type typeToInject, String name, InjectionMember[] injectionMembers) at Microsoft.Practices.Unity.Configuration.TypeInjectionElement.Configure(IUnityContainer container) at Microsoft.Practices.Unity.Configuration.UnityTypeElement.Configure(IUnityContainer container) at Microsoft.Practices.Unity.Configuration.UnityContainerElement.Configure(IUnityContainer container)

Does this mean I cant work with concrete classes through constructor injection or do the classes (all defined as public and instantiable (not static)) have to be decorated with some particular attributes ? I tried placing the [InjectionConstructor] attribute on the constructor for the type in question but that didnt have any effect.

Has anyone else come across this and have any suggestions? I can of course make all those classes in the constructor implement interfaces but that might be overkill as I already have literally every interface (around 15) in my app registered with unity !!

Thanks in advance,


Nov 22, 2009 at 2:03 AM

Unity works just fine with concrete classes. My guess would be that it's not actually building the class you think it is, or there's some configuration in the config file that's telling it to use the non-existent constructor. Are you using a configuration file? Is there something in the config file about this class?


Nov 24, 2009 at 5:22 PM
Edited Nov 24, 2009 at 5:22 PM

Hi Chris,

Thanks for getting back. The point about the setting in configuration rings a bell. I just noticed a setting in the file :

<type type="IHandlerResolver" mapTo="ConfigFileBasedHandlerResolver">
              <constructor />

I didnt pay attention to this earlier as I was swimming in a sea of typeAliases and mappings :-)

I guess thats the problem then?