Constructor Parameters (again)

Jun 23, 2008 at 8:58 AM

Sorry, I'm a Unity newbie and haven't found an answer (code) for the following simple issue:

If I've got a class with multiple constructors, I've understood that Unity takes the one with the longest signature and "tries" to resolve objects which have been passed as constructor parameters. I also understood, that if the parameter is e.g. a string object, Unity cannot resolve apropriately. I've read that there are methods to work around this and actually pass the string parameter. Can anybody provide the basic and stripped down CODE, that does just that based on the following type:







      class NullLogger : ILogger

        private string version;

            this.version = "1.0";

NullLogger(string v)
this.version = v;

void Log(string message)
Console.WriteLine("NULL> "+message);



Jun 23, 2008 at 3:02 PM
I might be getting the wrong end of the stick here... but are you after something like

using System;
using System.Configuration;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

namespace MSUnityStringConstructorSample
    class Program
        private static void Main(string[] args)
            IUnityContainer container = new UnityContainer();
            UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            ILogger nullLogger = container.Resolve<ILogger>();


    class NullLogger : ILogger
        private readonly string version;

        public NullLogger()
            version = "1.0";
        public NullLogger(string v)
            version = v;
        public void Log(string message)
            Console.WriteLine("NULL> " + version + " : " + message);

    public interface ILogger
            void Log(string message); 
With a configuration like this

<?xml version="1.0" encoding="utf-8" ?>
    <section name="unity"
                 Microsoft.Practices.Unity.Configuration, Version=,
                 Culture=neutral, PublicKeyToken=31bf3856ad364e35" />

          <type type="MSUnityStringConstructorSample.ILogger, MSUnityStringConstructorSample"
                mapTo="MSUnityStringConstructorSample.NullLogger, MSUnityStringConstructorSample">
            <typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration">     
                <param name="message" 
                       parameterType="System.String" >
                  <value value="2.0" 
<STYLE type=text/css> .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: Consolas, "Courier New", Courier, Monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } </STYLE>
I also have some other config based samples over at my blog if there any help






Jun 23, 2008 at 5:02 PM
You can do this either via config or via the API. MMcGuire above has kindly provided the configuration above.

Through the API, you'd do something like this:

IUnityContainer container = new UnityContainer()
    .RegisterType<ILogger, NullLogger>()
            new InjectionConstructor("some string"))

This configures the container to create an instance of NullLogger when resolving ILogger. When NullLogger is instantiated, it will call the constructor that takes a single string, and will pass the value "some string" to the constructor.

There's a ton of different stuff you can pass ConfigureInjectionFor to customize what is injected and with what values.

Jan 26, 2010 at 4:09 PM

You can also use the [InjectionConstructor] attribute on the constructor to mark the default constructor.

In this case the


public NullLogger() ...

This wil cause Unity to use this constructor you can then set the version via a property if the relevant property exists.


Jan 26, 2010 at 4:58 PM

Just to follow up with the current version of the container, the API call now looks like this:


IUnityContainer container = new UnityContainer()
    .RegisterType<ILogger, NullLogger>(
        new InjectionConstructor("some string"));

Just slightly smaller and easier than the old API. :-)