Configuring a proxy-server for WCF
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.