Default configuration for WCF service/client bindings is to use the default web proxy (the proxy-settings as configured in Internet Explorer).
To configure a proxy for a specific client/service endpoint, you can configure this on the binding that is used by the endpoint, eg:
<basicHttpBinding>
<binding name="MyClientBinding" proxyAddress="http://gateway:8080" useDefaultWebProxy="false">
</binding>
</basicHttpBinding>
This also works for custom bindings, eg:
<customBinding>
<binding name="MyCustomClientBinding">
<binaryMessageEncoding />
<httpTransport proxyAddress="http://gateway:8080" useDefaultWebProxy="false" />
</binding>
</customBinding>
It's also possible to configure the default webproxy in the app.config itself and leave all bindings unmodified. In this case, you may want to specify that local addresses should, or should not bypass the proxy-server, eg:
<system.net>
<defaultProxy useDefaultCredentials="true">
<proxy bypassonlocal="False" proxyaddress="http://gateway:8080" />
</defaultProxy>
</system.net>
This all works pretty transparent, but today I ran into some strange behavior: I wanted to inspect the HTTP-requests my client sent using Fiddler, but no matter how or where I configured the proxy-settings, they didn't seem to have any effect at all; WCF always bypassed the proxy. After reading lots of forums/blogs/etc, I finally found out the reason: I used localhost in the endpoint address!
It turned out that when localhost is used as the target machine name in the endpoint address, WCF/System.Net itself decides to ignore proxy-settings (probably for internal routing/performance reasons). After using the actual machinename in the address, everything suddendly worked like a charm!
Since Fiddler automatically registers itself as the default web proxy (in IE) on startup, I didn't have to configure anything in WCF at all to get things working, as long as I didn't use localhost in the endpoint-addresses.
3 comments
Very usefull article.
How to config proxy username and password for basic auth?
Bodya
Thanks for the info on the fact that it doesn’t work if you use the host name localhost. I was scratching my head on that one also. I submitted a bug report on it because I feel that that is very confusing behavior and should be changed. Please rate my bug at the following address if you agree.
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=358592
Jon
For WCF, I find that it is necessary to set bypassProxyOnLocal=’false’ if the WCF server is the same machine as the calling application (the one where this web.config resides) in addition to using explicit host name instead of localhost.
K.Kong