【问题标题】:How to use Dependency.OnValue to differentiate between 2 parameters with the same name but different types如何使用 Dependency.OnValue 区分具有相同名称但不同类型的 2 个参数
【发布时间】:2014-05-07 05:30:03
【问题描述】:

我在尝试使用自动生成的 Web 服务客户端时遇到了一个有趣的场景。这意味着我不能修改其他类的构造函数。 它有 2 个构造函数,如下所示:-

public FooServicePortTypeClient(string endPointConfigurationName, string remoteAddress)
public FooServicePortTypeClient(string endPointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress)

注意参数名称“remoteAddress”是如何使用两次的,但是对于一个构造函数来说它是一个字符串,而对于另一个它是一个 EndpointAddress 对象。

我试图通过具有内联依赖项的 Windsor 容器注册我的 Web 服务,方法是使用 DependsOn 来传递服务客户端的参数,如下所示:-

container.Register(Component.For<FooServicePortTypeClient>().DependsOn(Dependency.OnValue("endPointConfigurationName", "fooEndpointConfigname"), Dependency.Onvalue("remoteAddress", "fooRemoteAddress"))));

然后我注意到 Castle Windsor 试图将 remoteAddress 的字符串与带有类型参数 EndpointAddress 的构造函数匹配(因此失败),而不是选择字符串版本。

有没有办法真正区分这 2 个“不同”参数,以便我可以选择使用带字符串的参数或带 EndpointAddress 的参数?

我发现我只能用 Dependency.OnValue 指定参数名称,尝试键入版本没有意义,因为构造函数中有 2 个字符串(endPointConfigurationName 和 remoteAddress),它无法区分它们。

就个人而言,这是微不足道的,我绝对可以通过向它提供 EndpointAddress 的新对象来完成 Castle Windsor 正在寻找的事情。 但是,我只是想知道温莎城堡如何决定与 Dependency.OnValue 一起使用的构造函数版本,以及是否真的有办法让它更喜欢其中一个。当然,我认为对于像我这样希望字符串版本能够正常工作的没有经验的用户来说,这是一个陷阱。

【问题讨论】:

    标签: c# castle-windsor


    【解决方案1】:

    Dependency.OnValue 具有允许您提供类型的重载:

    Dependency.OnValue<System.ServiceModel.EndpointAddress>(endpointAddress)
    

    这告诉 Windsor 解析“更高级别”的类型(与 stringint 等基本类型相比)。有关详细信息,请参阅docs

    【讨论】:

    • 好的,我做到了,再试一次。实际上,只需指定第二个参数的类型,它就会自动查找第二个参数匹配为字符串的构造函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-24
    • 1970-01-01
    • 2019-09-30
    • 1970-01-01
    • 1970-01-01
    • 2018-08-18
    • 2016-12-02
    相关资源
    最近更新 更多