如何在 WCF 客户端服务代理上设置 HTTP 代理 (WebProxy)?

发布于 2024-07-23 08:34:58 字数 1801 浏览 7 评论 0原文

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

代理,代理,代理。

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

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

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

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

我可以从命令行中进行设置 WinXP / 2003 使用此处所述的 ProxyCfg.exe,以及稍后的内容 带有 netsh.exe 的 Windows 版本如此处所述

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

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

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

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

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

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

相关:
- 如何设置-proxy-with-credentials -to- generated-wcf-client

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

Proxies, proxies, proxies.

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); 

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.

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.

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

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?

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); 

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?

Related:
- how-to-set-proxy-with-credentials-to-generated-wcf-client

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我一向站在原地 2024-07-30 08:34:59

您还可以尝试以下操作:

以编程方式获取您正在使用的任何绑定,然后直接在其上设置代理,例如,

var binding = new WSDualHttpBinding("WSDualHttpBinding_IMainService");
binding.ProxyAddress = new Uri("http://192.168.5.1:3128");

其中 "WSDualHttpBinding_IMainService" 是配置文件中的绑定名称。
此外,您还必须设置UseDefaultWebProxy=false; 否则你的代理将被忽略。

You could also try this :

Programmatically get whatever binding you are using,and then set the proxy on it directly e.g.

var binding = new WSDualHttpBinding("WSDualHttpBinding_IMainService");
binding.ProxyAddress = new Uri("http://192.168.5.1:3128");

where "WSDualHttpBinding_IMainService" is the name of your binding from your config file.
Also you have to set UseDefaultWebProxy=false; otherwise your proxy will be ignored.

溺渁∝ 2024-07-30 08:34:59

我为可能使用 .NET Core 2.x 的人找到了一个解决方案

,最初在尝试使用设置代理时会导致问题

System.ServiceModel.BasicHttpBinding;

正如 Cheeso 所回答的。

您必须将 WCF 更新到最新版本 (v.4.7.0)。

转到 NuGet 包管理器 -> 更新 WCF 的所有相关项目 URL。

必须有:

  • System.ServiceModel.Security
  • System.ServiceModel.NetTcp
  • System.ServiceModel.Http

I found a solution for someone who might use .NET Core 2.x

Which initially cause an issue when trying to set proxy by using

System.ServiceModel.BasicHttpBinding;

as answered by Cheeso.

You have to update WCF to the latest (v.4.7.0).

Go to NuGet Package Manager -> Update all related Project URL of WCF.

There must be:

  • System.ServiceModel.Security
  • System.ServiceModel.NetTcp
  • System.ServiceModel.Http
橘虞初梦 2024-07-30 08:34:58

WCF 代理上没有 Proxy 属性是有道理的,因为并非所有 WCF 代理都使用 HTTP 进行通信。 经过进一步审查,我发现如果 WCF 代理使用 HTTP 绑定,则可以通过编程方式在 WCF 中设置代理。 我在这里记录它,以防其他人需要它。 要在代码中为 WCF 客户端设置 HTTP 代理,请执行以下操作:

// instantiate a proxy for the service
var svc= new ServiceClient();
// get the HTTP binding
var b = svc.Endpoint.Binding as System.ServiceModel.BasicHttpBinding;
b.ProxyAddress = new Uri("http://127.0.0.1:8888");
b.BypassProxyOnLocal = false;
b.UseDefaultWebProxy = false;

要在代码中设置端点地址(到达服务器的位置),您将执行如下操作:

var e = svc.Endpoint;
e.Address = new System.ServiceModel.EndpointAddress(
    "http://remoteserver:5555/WcfXmlElement");

It makes sense that there is no Proxy property on the WCF proxy, because not all WCF proxies use HTTP for communication. After further review, I found that it is possible to set the proxy in WCF programmatically, if the WCF proxy uses an HTTP binding. I am documenting it here in case someone else needs it. To set the HTTP Proxy in code for a WCF client, do this:

// instantiate a proxy for the service
var svc= new ServiceClient();
// get the HTTP binding
var b = svc.Endpoint.Binding as System.ServiceModel.BasicHttpBinding;
b.ProxyAddress = new Uri("http://127.0.0.1:8888");
b.BypassProxyOnLocal = false;
b.UseDefaultWebProxy = false;

And to set the endpoint address - where to reach the server - in code, you would do something like this:

var e = svc.Endpoint;
e.Address = new System.ServiceModel.EndpointAddress(
    "http://remoteserver:5555/WcfXmlElement");
放肆 2024-07-30 08:34:58

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

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

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

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.

淡写薰衣草的香 2024-07-30 08:34:58

我遇到了类似的问题,但我还需要使用与访问服务所用的用户名和密码不同的代理用户名和密码。

我尝试通过 UriBuilder 构建它,它将代理地址输出为“http://username:password@myproxyserver/< /a>”。 不幸的是,我使用的特定代理不适用于此技术。

经过大量谷歌搜索后我发现,您可以通过 WebRequest.DefaultProxy (静态属性)更改代理。

例如:

WebProxy proxy = new WebProxy("http://myproxyserver",true);
proxy.Credentials = new NetworkCredential("username", "password");
WebRequest.DefaultWebProxy = proxy;

I have had a similar problem, but I also needed to use a username and password for the proxy that differ from the username and password used to access the service.

I tried building it up through a UriBuilder, which would output the proxy address as "http://username:password@myproxyserver/". Unfortunately, the particular proxy I was using didn't work with this technique.

What I found after extensive Googling, is that you can change the proxy through WebRequest.DefaultProxy (static property).

For example:

WebProxy proxy = new WebProxy("http://myproxyserver",true);
proxy.Credentials = new NetworkCredential("username", "password");
WebRequest.DefaultWebProxy = proxy;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文