如何一次为多个 WCF 绑定配置代理地址?

How to configure proxy address for multiple WCF-bindings at once?

要在 WCF 中配置代理设置,我可以这样解决:

<binding name="bindingName" proxyAddress="http://proxySomething">

但是,这需要复制到您拥有的每个绑定中。我想知道是否有一种方法可以将 all 绑定设置在一个地方。类似于:

<proxy address="http://proxySomething" forAllBindings="true"/>

有这样的设置吗?我试过搜索,但没找到。

更新1: 还需要注意的是,我有多种类型的绑定(basicHttpBindingcustomBindingwsHttpBindingwebHttpBinding) 和一些类型的几个命名绑定。

如果您使用的是 .net 4.5,那么您可以使用简化的配置样式 WCF bindings。这使您可以配置特定的绑定类型,然后当您的客户端或服务使用该特定绑定类型(例如 basicHttpBinding)时,将使用修改后的配置。

要修改默认绑定,不要指定名称即可。

所以

<bindings> <basicHttpBinding> <binding messageEncoding="Mtom" /> </<basicHttpBinding> </bindings>

现在 如果您使用 basicHttpBinding 创建服务,它将使用 Mtom 而不是 Text。

您可以使用网络设置中的 defaultProxy 元素:

<configuration>
  <system.net>
    <defaultProxy>
      <proxy proxyaddress="http://someproxy:8080"/>
    </defaultProxy>
  </system.net>
</configuration>

信息支持中的更多详细信息blog post

请注意,这是一个全局代理和 it used by WebRequest 个实例。