Cannot resolve Dictionary in Unity container

Posted by IanR on Stack Overflow See other posts from Stack Overflow or by IanR
Published on 2010-04-29T00:01:05Z Indexed on 2010/04/29 0:07 UTC
Read the original article Hit count: 598

Filed under:
|
|

Hi,

I've just stumbled upon this:

within a Unity container, I want to register IDictionary<TK, TV>; assume that it's IDictionary<string, int>

_unityContainer = new UnityContainer()
    .RegisterType<IDictionary<string, int>, Dictionary<string, int>>();

but if I try

var d = _unityContainer.Resolve<IDictionary<string, int>>();

it fails to resolve...

I get...

Microsoft.Practices.Unity.ResolutionFailedException: Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = "System.Collections.Generic.IDictionary`2[System.String,System.Int32]", name = "(none)". Exception occurred while: while resolving.

Exception is: InvalidOperationException - The type Dictionary`2 has multiple constructors of length 2. Unable to disambiguate.


At the time of the exception, the container was:

Resolving System.Collections.Generic.Dictionary2[System.String,System.Int32],(none) (mapped from System.Collections.Generic.IDictionary2[System.String,System.Int32], (none)) ---> System.InvalidOperationException: The type Dictionary`2 has multiple constructors of length 2. Unable to disambiguate..

So it looks like it has found the Type to resolve (being Dictionary<string, int>) but failed to new it up...

How come unity can't resolve this type? If I type

IDictionary<string, int> d = new Dictionary<string, int>()

that works...

any ideas?

thanks!

© Stack Overflow or respective owner

Related posts about c#

Related posts about ioc-container