如何在 WCF 客户端服务代理上设置 HTTP 代理 (WebProxy)? [英] How can I set an HTTP Proxy (WebProxy) on a WCF client-side Service proxy?

查看:26
本文介绍了如何在 WCF 客户端服务代理上设置 HTTP 代理 (WebProxy)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 WCF 客户端上以编程方式设置 HTTP 代理,而不使用默认代理?

How can I set the HTTP proxy programmatically, on a WCF client, without using the default proxy?

代理,代理,代理.

根据WCF的开发模型,我生成客户端代理"通过在服务的 WSDL 上运行 svcutil.exe 来创建类.(这也是生成客户端配置文件).

According to the WCF model of development, I generate client-side "proxy" classes by running svcutil.exe on the WSDL for the service. (This also produces a client-side config file).

在我的代码中,我新建了该类的一个实例,并且可以连接到服务.很不错.

In my code I new-up an instance of that class and I can connect to the service. Very nice.

var svcProxy = new MyWebService();
svcProxy.GetInformation(request); 

我们称这个东西为代理类,但还有另一个代理——http 代理.这服务使用的是wsHttpBinding basicHttpBinding,所以就过去了http.现在,假设我想通过以下方式将客户端连接到 Web 服务http 代理(由 .NET BCL 中的 System.Net.WebProxy 建模).我知道从我阅读 .NET 和 WCF 文档的广泛而愉快的经历来看,WCF 运行时,如果没有另外指示,将使用默认值通过 http/https 通信时的系统代理.

We call this thing a proxy class, but there is another proxy - the http proxy. This service is using wsHttpBinding basicHttpBinding, so it is going over http. Now, suppose I want to connect the client to the web service over a http proxy (modeled by a System.Net.WebProxy in the .NET BCL). I know from my extensive, delightful experience reading .NET and WCF documentation, that the WCF runtime, if not instructed otherwise, will use the default system proxy when communicating over http/https.

我可以从命令行设置WinXP/2003 使用此处描述的 ProxyCfg.exe,以及稍后Windows 版本 和 netsh.exe如此处所述.

I can set that from the command line in WinXP / 2003 with ProxyCfg.exe as described here, and in later versions of Windows with netsh.exe as described here.

我还可以指定在应用程序中使用的默认网络代理通过设置 System.Net.WebRequest.DefaultWebProxy 属性.

I can also specify the default web proxy for use within the application by setting the System.Net.WebRequest.DefaultWebProxy property.

但是假设我想通过一个与代理不同的代理进行连接全系统代理?例如,也许没有系统范围的代理,但是我需要特别为 Web 服务使用一个.或者也许有一个系统范围的代理,但我需要为网络使用不同的代理服务.事实上,可能有多个 Web 服务客户端,并且每个人都应该得到一个不同的代理.

But suppose I want to connect over a proxy that is different than the system-wide proxy? For instance maybe there is no system-wide proxy but I need to use one for the web service in particular. Or maybe there is a system-wide proxy but I need to use a different one, for the web service. And in fact maybe there are multiple web service clients, and each one should get a different proxy.

如何设置每个绑定的代理?

How can the proxy be set per-binding?

在 ASMX 模型中,我可以这样做:

In the ASMX model, I could do this:

var svcProxy = new MyWebService();
svcProxy.Proxy = new System.Net.WebProxy("http://proxyserver:1234", true);
svcProxy.GetInformation(request); 

但这在 WCF 中是不可能的;WCF 生成的客户端代理类不公开 Proxy 属性.如何设置每个客户端代理的 http 代理,以及如何在 http 代理上设置身份验证?

But this is not possible with WCF; the WCF-generated client-side proxy classes do not expose a Proxy property. How do I set the http proxy, per client-side proxy, and how do I set authentication on the http proxy as well?

相关:
-
how-to-set-proxy-with-credentials-to-generated-wcf-client

推荐答案

代理设置是绑定配置的一部分.例如,查看 ProxyAddress 属性BasicHTTPBindingWSHttpBinding 类/配置元素.

The proxy settings are part of the binding configuration. For example, look at the ProxyAddress property of the BasicHTTPBinding and WSHttpBinding classes/configuration elements.

看起来您将端点配置保留在 app.config 文件中,在这种情况下,您应该能够在那里设置地址.

Looks like you're leaving your endpoint configuration in the app.config file, in which case you should be able to set the address there.

这篇关于如何在 WCF 客户端服务代理上设置 HTTP 代理 (WebProxy)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆