Search code examples
c#castle-windsor

Windsor register singleton component for multiple interfaces


I want to register one class with 2 interfaces in Castle.Windsor.

does this code work... Will I have only one instance for both interfaces...

Component.For<IEnvironment>().ImplementedBy<OutlookEnvironment>().LifestyleSingleton()
Component.For<IOutlookEnvironment>().ImplementedBy<OutlookEnvironment>().LifestyleSingleton()

I need to double check this because my environment should always be the same instance...

So when I resolve using the IEnvironment interface I should get the same instance as when using IOutlookEnvironment to resolve the component


Solution

  • You need to use the use multi-generic-parameter overload of the Component.For method

    Component.For<IEnvironment, IOutlookEnvironment>()
             .ImplementedBy<OutlookEnvironment>()
             .LifestyleSingleton()
    

    See also in the documentation: Registering component with multiple services section.