tags:

views:

136

answers:

1

I seem to have a problem - rather unexpected; so I guess I might be doing something silly/wrong.

I register two container scoped services as such:

builder.Register<IServiceInfo>(c => CreateServiceInfo(c)).As<IServiceInfo>();
builder.Register<IServiceInfo>(c => CreateServiceInfoSomeOther(c)).As<IServiceInfo>().Named("someOther");

Now when I try to resolve

container.Resolve<IServiceInfo>()
and container.Resolve<IServiceInfo>("someOther")

I get the same instance. I would expect the first call to return the first instance and the second call to return the second instance. Why is this happening this way?

I am hoping there are some active Autofac experts around here and I'd appreciate any help to quickly fix this.

Thanks, all!

+1  A: 

When two services are registered, the last one wins (it doesn't matter that the second is named, it's still a registration for that service), unless you ask for an instance by name.

You can use ".DefaultOnly()" to modify your second registration so that the first is the actual default. See the wiki page for more info.

Philip Rieck
Great, that did the trick. Thanks a ton!
Charles Prakash Dasari