开发者

How do I make my WCF service call go through a configured local proxy server?

I've written a class that's responsible for calling a third party WCF serv开发者_高级运维ice and it works fine. Our application can be configured with the proxy settings of the local network, so all that remains is to use those settings (if they have been set) when making the call.

I've had a look around and can't see anything that clearly states how to do this. I found that the BasicHttpBinding object that I'm using has a ProxyAddress property, but there's nothing there that lets me define network credentials or a username and password.

It's probably right here in front of me, so I think I just need pointing in the right direction. At least I'm hoping it's that simple! All I need to do is tell my service call what the local proxy server is and what credentials to use.

This is the code I have that instantiates the service class. I've omitted the next part which builds the request class, calls the service method and processes the result.

// Create the service instance.
var binding = new BasicHttpBinding();
var endPoint = new EndpointAddress(new Uri(_servicesBaseUri + "MyServiceName"));
var service = new WSHsgCreateSchemeRepairClient(binding, endPoint);

// Add the MessageInspector to the contract behaviours list. This will inject the SecurityHeader XML and the SOAP action.
var soapAction = _servicesBaseUri + "MyServiceName/MyServiceMethod";
service.Endpoint.Contract.Behaviors.Add(new MessageInspector(_securityHeaderUsername, _securityHeaderPassword, soapAction));

We have some other web service calls (that use the old web service approach) and they generate a new instance of System.Net.WebProxy and set it against the service class Proxy property, but obviously it's different in WCF.

Thanks.


I think I've worked it out. The default system proxy can be set and used. This is a very useful article if anyone else is up against the same requirement.

The idea is that you have to first set System.Net.HttpWebRequest.DefaultWebProxy.Credentials (presumably System.Net.CredentialCache.DefaultCredentials) - that's where "default proxy" parameters are taken from when you set UseDefaultWebProxy of the binding to true.


You can do this through the web.config setting specific to your service. In the binding config, set proxyAddress="http://myproxy:8080" and set useDefaultWebProxy="false"

<bindings>
  <basicHttpBinding>
    <binding name="SubscriberFulfilmentServiceSOAP12Binding" closeTimeout="00:01:00"
    openTimeout="00:01:00" receiveTimeout="00:01:00" sendTimeout="00:01:00"
    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
    maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
    textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="false"
    proxyAddress="http://myproxy:8080"
    messageEncoding="Text">
      <readerQuotas maxDepth="32" maxStringContentLength="2147483647"
      maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      <security mode="None">
        <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
        <message clientCredentialType="UserName" algorithmSuite="Default" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>

To set the proxy for all services:

<system.net>
  <defaultProxy>
    <proxy usesystemdefault="True" proxyaddress="http://myproxy:8080" bypassonlocal="True" />
  </defaultProxy>
</system.net>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜