【问题标题】:Castle always load the first component when resolving a serviceCastle 在解析服务时总是加载第一个组件
【发布时间】:2015-03-11 03:45:07
【问题描述】:

我有两个用于相同服务类型的组件。在运行应用程序时,Castle 总是加载第一个组件。例如,我有一个指向不同数据库的IDbConfiguration 服务,以及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>

组件PlayerRepository 依赖于DbReportingConfigPlayerDomainService 依赖于PlayerRepository,但是当我解决这些服务中的任何一个时,它们总是加载第一个DbConfig

更改 xml 文件中的顺序显示 IDbConfiguration 的第一个组件是每次使用的组件。我为这两个组件设置了不同的 Id,为什么总是加载第一个?

【问题讨论】:

    标签: components castle


    【解决方案1】:

    这种行为是预期的:来自docs

    在 Windsor 中,第一个获胜 In Castle,默认实现 service 是第一个注册的实现。这不同于 例如 AutoFac,默认是最后注册的 执行 (http://code.google.com/p/autofac/wiki/ComponentCreation)。

    现在,如果您想在特定上下文中解析特定组件,您应该查看 service overridedependencies(我更熟悉后者,我觉得更明确)。

    我不在 Windsor 中使用 xml 配置,但 this page 应该可以帮助您设置服务覆盖

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-22
      • 2018-05-27
      • 1970-01-01
      • 2016-03-14
      • 1970-01-01
      • 2017-12-08
      • 1970-01-01
      • 2013-02-04
      相关资源
      最近更新 更多