2
votes

Using Castle Windsor I can register multiple implementations of a type e.g.

container.Register(Component.For<IMyInterceptor>()
    .ImplementedBy<MyInterceptor>();

container.Register(Component.For<IMyInterceptor>()
    .ImplementedBy<MyInterceptor2>();

This all works as you'd expect and I can resolve multiple implementations using ResolveAll()

If I try to register an instance of another implementation at runtime though - e.g.

var interceptor = new MyInterceptor3();
Container.Register(
    Component.For<IMyInterceptor>()
        .Instance(interceptor));

I get an exception:

There is a component already registered for the given key MyInterceptor3

Is this the expected behaviour? What I'm trying to acheive is to use e.g. Container.ResolveAll() to get a list of default implementations of IMyInterceptor plus optional additional implementations (i.e. on a request by request basis, when debug modes are enabled).

1
My collegue pointed me to this link stw.castleproject.org/… Specifically: Registering instance ignores lifestyle When you register an existing instance, even if you specify a lifestyle it will be ignored. Also registering instance, will set the implementation type for you, so if you try to do it manually, an exception will be thrown. So it looks like instances are always Singleton in nature (?)Simon Thorogood

1 Answers

1
votes

This will sort you out.

var interceptor = new MyInterceptor3();
Container.Register(
    Component.For<IMyInterceptor>()
        .Instance(interceptor).Named("something unique));

Names must be unique.