Search code examples
c#inversion-of-controlcastle-windsor

Castle Windsor: SelectHandler should be called only during resolving?


I am using Castle Windsor 4.1.1. For simplicity I have created demo to illustrate the problem:

This is container initialization:

using(var container = new WindsorContainer())
{
    container.Kernel.AddHandlerSelector(new HandlerSelector());

    // Should not call handler selector
    container.Register(Component.For<IA>().ImplementedBy<AImpl>());
    container.Register(Component.For<IB>().ImplementedBy<BImpl>());

    // Should call handler selector
    container.Resolve<IA>();
}

AImpl depends on IB. (Just constructor injection)

When I call container.Register it calls HasOpinionAbout, and SelectHandler.

Question is pretty straightforward. Is it supposed to work like that?

Here is quote from official documentation:

Handler selectors let you dynamically choose a component to satisfy the requested service and override Windsor's default behavior. This is particularly useful in multi-tenant applications.

I was expecting that these functions should get called only during resolving not during registration. (They get called both during resolving and registration.)

They cleary state that it is usefull for multi-tenant applications, so in most cases tenant is determined during resolving, not during registration.

Is my assumption correct?

GitHub Issue #461


Solution

  • You need to use a different lifestyle (than Singleton, which is the default) if you want to be able to have different component instances for different tenants.