Himadri Mishra
Himadri Mishra

Reputation: 33

WCF service call from class project with proxy

I need to make a WCF service call to an external 3rd party using 2-way SSL from a class project. I have added the WSDL provided by the 3rd party to my project as a Service Reference. The problem is that all calls outside our domain (*.abc.com) pass through a proxy server

http://ironport:8080

This is what I have done in my code -

var binding = new WSHttpBinding();
binding.Security.Mode = SecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
binding.BypassProxyOnLocal = false;
binding.UseDefaultWebProxy = true;
binding.AllowCookies = false;
binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;

var endpoint = new EndpointAddress("https://blablabla.com/GetData.svc");
var client = new AccountClient(binding, endpoint);
X509Certificate2 certi = new X509Certificate2(@"path to pfx file", "password");
client.ClientCredentials.ClientCertificate.Certificate = certi;

I make the service call using -

var account = client.ExportAccounts(obj1, obj2, obj3);

It then gives me an error -

The remote server returned an Error (407): Proxy authentication required

That is but obvious because nowhere did I mention the proxy details the request needs to go through. What I need is a way to add the following info from a web.config file of a different project into my request above -

<system.net>
<defaultProxy useDefaultCredentials="true">
  <proxy proxyaddress="http://ironport:8080" />
  <bypasslist>
    <add address="[\w]+\.abc\.com$" />
  </bypasslist>
</defaultProxy>
</system.net>

Is there some way to achieve this in code? Or do I need to go about this in a different way altogether? Let me know if I need to post more information.

Upvotes: 1

Views: 1323

Answers (1)

Tim
Tim

Reputation: 28530

You could try using the WebProxy Class. Not tested, but something like this:

WebProxy proxy = new WebProxy("http://ironport:8080");
proxy.BypassList = new string[] { "[\w]+\.abc\.com$" };

Another alternative would be to move the relevant section of the config to the web/app.config of the application that is using your class library.

ADDED

Not 100% sure this will work, but you could try adding this line to your code:

WebRequest.DefaultProxy = proxy;

Taken from this answer

Another option might be to use the ProxyAddress property of WsHttpBinding (make sure in that case you set the UseDefaultProxy to false), but I don't see a way to add a bypass list with this one.

Upvotes: 1

Related Questions