Search code examples
componentscastle

Castle always load the first component when resolving a service


I have two components for the same service type. When running the application,Castle always load the first component. For example I have one IDbConfiguration service which points to different Databases, and two components for IDbconfiguration:

<component id="DbConfig" 
    service="Maloft.Keryx.Infrastructure.RepositoryFramework.IDbConfiguration, Keryx.Infrastructure"
    type="Maloft.Keryx.Infrastructure.RepositoryFramework.DbConfiguration, Keryx.Infrastructure"
    lifeStyle="PerWebRequest"/>
    <parameters>
        <connectionName>kerxy</connectionName>
    </parameters>
<component id="DbReportingConfig"                    
    service="Maloft.Keryx.Infrastructure.RepositoryFramework.IDbConfiguration, Keryx.Infrastructure"          
    type="Maloft.Keryx.Infrastructure.RepositoryFramework.DbConfiguration, Keryx.Infrastructure"
    lifeStyle="PerWebRequest">
    <parameters>
        <connectionName>kerxyReporting</connectionName>
    </parameters>
</component>

The component PlayerRepository depends on DbReportingConfig, PlayerDomainService depends on PlayerRepository, but when i resolve any of these service they always load the first DbConfig.

Changing the order in the xml file shows that the first component for IDbConfiguration is the one used each time. I have set different Ids for these two components, why is the first one always loaded?


Solution

  • That behavior is expected: from the docs

    In Windsor first one wins In Castle, the default implementation for a service is the first registered implementation. This is different from AutoFac for example, where the default is the last registered implementation (http://code.google.com/p/autofac/wiki/ComponentCreation).

    Now if you want to resolve a specific component in a specific context you should take a look at service override or dependencies (I'm more familiar with the latter, which I find more explicit).

    I don't use xml configuration in Windsor but this page should help you setting up service overrides