Search code examples
castle-windsorcastle

Castle Windsor - Register components only once


I was wondering if there is some best practice to achive the functionality I need.

I have a web application that during startup, with reflection, scans certain assemblies (plugins) and registers their dependencies against common kernel.

The external libraries may need same dependencies.

For example library A gets scanned and its dependecy D1 is registered. Then library B is scanned and tries to register same dependency - D1 - against kernel again. Castle obviously will complain if I try to register same component twice.

My question is: How you usually handle the scenario when you simply want to skip the component that is already registered. Is there some extension method on IWindsorContainer that performs this task?

Thanks in advance.


Solution

  • You could use inside your register statement:

    Component.For<Test>.OnlyNewServices()
    

    or when registering multiple components at once:

    Types.FromAssemblyContaining<Test>().InSameNamespaceAs<Test>().Configure(c => c.OnlyNewServices());
    

    Or a variation that suits you. See this link for conditional registration: http://docs.castleproject.org/Windsor.Conditional-component-registration.ashx