[ACCEPTED]-How can I set an HTTP Proxy (WebProxy) on a WCF client-side Service proxy?-webproxy

Accepted answer
Score: 66

It makes sense that there is no Proxy property 10 on the WCF proxy, because not all WCF proxies 9 use HTTP for communication. After further 8 review, I found that it is possible to set 7 the proxy in WCF programmatically, if the 6 WCF proxy uses an HTTP binding. I am documenting 5 it here in case someone else needs it. To 4 set the HTTP Proxy in code for a WCF client, do 3 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 2 to reach the server - in code, you would 1 do something like this:

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

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

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

Score: 22

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

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

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

For example:

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

You could also try this :

Programmatically 5 get whatever binding you are using,and then 4 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 3 the name of your binding from your config 2 file. Also you have to set UseDefaultWebProxy=false; otherwise your 1 proxy will be ignored.

Score: 2

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

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

System.ServiceModel.BasicHttpBinding;

as 4 answered by Cheeso.

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

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

There must be:

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

More Related questions