Rapture in Everything

Debugging WCF service traffic with Fiddler

zblesk

At work, I had to look into request/response compression in WCF with .NET 3.5. In order to be sure whether what I’m doing had had the desired effect, I had to force Fiddler to capture the client-server SOAP traffic. So now I’ll try to summarize what I learned.

The process consists of four steps:

  1. Hosting the web service
  2. Setting up Fiddler reverse proxy
  3. Modifying the WS’ WSDL file
  4. Setting up the client
  5. Calling the service via Fiddler’s proxy

First, you need to host your service somewhere. In my example, I used the IIS 7.5 and set it to listen locally at port 555.

Then you have to set up the reverse proxy, which gathers requests from various sources (servers) and forwards them to its client (in this case, your service). In this scenario, this is useful because you might have difficulties routing the communication between your testing client (be it the default WCF Test Client, SoapUI, a smartphone or what have you) and the service.

To set it up in Fiddler, you first have to go to Tools | Fiddler options | Connections. Set the proxy port to the desired value; in my case, 8888.

Then you have to forward the traffic, coming to the proxy, to the WCF service in question. Go to Rules | Custom Rules. In the OnBeforeRequest method, add the redirecting.

if (oSession.host.toLowerCase() == "localhost:8888") oSession.host = "localhost:555";

You’re almost ready to go. Now the only remaining obstacle is the definition (WSDL) file your service generates. The .svc automatically generates a WSDL that contains the endpoint address in a few places; the local address is automatically inserted. If you tried to create a reference using this WSDL, your WCF Test Client would go directly to the service instead of going through the proxy. So copy the WSDL file, replace all original endpoints with your proxy (in this case, localhost:8888) and save it; then give it to the Test Client to create the reference from.

WCF Test Client

And that’s it, you’re set. You can call the service and any all communication will be logged by Fiddler and forwarded to your service hosted on the IIS.