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

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

Leave a Comment