views:

323

answers:

1

I'm trying to register the same type but with two different constructors. When I trying to resolve, I get "Resolution of the dependency failed" on the second Resolve.

    var container = new UnityContainer();

    container.RegisterType<IBar, Bar>()
        .RegisterInstance(new Bar())
        .RegisterType<IBar, Bar>()
        .RegisterInstance(new Bar("foo"));

    Bar bar1 = (Bar)container.Resolve<IBar>();
    Bar bar2 = (Bar)container.Resolve<IBar>("foo");  // ERROR

What I'm doing wrong?

+3  A: 

You need to give them names when registering. The parameter to Resolve is the name of the instance you want.

var container = new UnityContainer();

container
    .RegisterInstance<IBar>("BAR", new Bar())
    .RegisterInstance<IBar>("FOOBAR", new Bar("foo"));

Bar bar1 = (Bar)container.Resolve<IBar>("BAR");
Bar bar2 = (Bar)container.Resolve<IBar>("FOOBAR");
tpower
Can you give me an example please.
Vadim
Now I have the same error when resolving the first one.
Vadim
Second version works just fine. Thanks a lot.
Vadim