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

查看:324
本文介绍了如何在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.

我还可以通过设置中使用的默认Web代理rel =nofollow noreferrer> System.Net.WebRequest.DefaultWebProxy属性

但是假设我想通过与
系统范围代理不同的代理连接?例如,可能没有系统范围的代理,但是
我需要特别使用一个用于Web服务。或者可能有
系统范围的代理,但我需要使用另一个,用于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 属性 BasicHTTPBinding WSHttpBinding 类/配置元素。

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天全站免登陆