tags:

views:

41

answers:

1

I have a concrete class of logger that Microsoft.Unity will Resolve ILogger to.

the logger class has two constructors

1. public logger()

2. public logger(List<BaseLogger> Loggers)

I have the unity section in my web.config file looking like this.

<unity>
<containers>
  <container>
    <types>

      <type type="MyCompany.Logger.ILogger, MyCompany.Logger"
           mapTo="MyCompany.Logger.logger, MyCompany.Logger">

      </type>
    </types>
   </container>
  </containers>
</unity>

If I remove the constructor listed as 2. this logger class will succeed being Resolved by Unity. When I add it back I get this error...

Resolution of the dependency failed, type = "MyCompany.Logger.ILogger", name = "(none)". Exception occurred while: while resolving. Exception is: InvalidOperationException - The type List`1 has multiple constructors of length 1. Unable to disambiguate.

Can anyone explain how I can change my web.config entry to handle having the 2. listed overloaded constructor in logger to work while trying to Unity.Resolve?

+1  A: 

Unity by default tries to create instance using the constructor with maximum number of parameters.

In order to specify which one to use, you need to make use of constructor injection. You can either mark the constructor to be used with InjectionConstructor attribute or specify constructor in the configuration. this should help.

I understand the documentation on MSDN isn't good enough. See this if it helps. I am not able to find the link to the article I had read today at office. That had quite good description. Good luck if you can find that one or a better one.

danish
I see from the document you linked me to how to handle a simple type like a string for putting parameters in a web.config file. What I am now having trouble seeing from the documentation is how do this with a more complex type like the List<BaseLogger> that I mentioned.
apolfj