Ivan Ivan - 4 months ago 14
C# Question

Cant resolve instance with Unity container

I register an instance with Unity like this:

ContentSlideControlsEntity contentSlideControlsEntity = new ContentSlideControlsEntity(new Queue<uint>());
container.RegisterInstance(typeof(ContentSlideControlsEntity), "contentSlideControlsEntity", contentSlideControlsEntity);


And then I simply want to reslove it:

ContentSlideControlsEntity contentSlideControlsEntity2 = container.Resolve<ContentSlideControlsEntity>();


but I get the following runtime error:


Microsoft.Practices.Unity.ResolutionFailedException: 'Resolution of
the dependency failed, type =
"MSDataLayer.Entities.ContentSlideControlsEntity", name = "(none)".

Exception occurred while: while resolving.

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




At the time of the exception, the container was:

Resolving MSDataLayer.Entities.ContentSlideControlsEntity,(none)

Resolving parameter "slideIDQueue" of constructor
MSDataLayer.Entities.ContentSlideControlsEntity(System.Collections.Generic.Queue`1[[System.UInt32,
mscorlib, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089]] slideIDQueue)

Resolving System.Collections.Generic.Queue`1[System.UInt32],(none)


RB. RB.
Answer Source

You registered your instance as a named registration, but you are resolving the unnamed registration (which doesn't exist).

container.RegisterInstance(typeof(ContentSlideControlsEntity), "contentSlideControlsEntity", contentSlideControlsEntity);
                                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

You have 2 options:

1) Don't register it as a named registraion

container.RegisterInstance(typeof(ContentSlideControlsEntity), contentSlideControlsEntity);

2) Resolve it with a name

container.Resolve<ContentSlideControlsEntity>("contentSlideControlsEntity");