Fiddler is an awesome tool for debugging web services. For the last 10 years, I’ve been using it virtually every working day for web service debugging in scenarios such as:

  • Capturing HTTP requests as they travel through a distributed system, which you have set up on your local development machine.
  • Intercepting and modifying HTTP requests, essentially performing a local man-in-the-middle interception. This is particularly useful if want to modify HTTP requests to simulate functionality which isn’t currently present, modify HTTP headers, etc.
  • Replaying captured requests - useful if you are debugging the way a web service behaves, given a specific request; capture the request, and replay it as many times as you need in order to debug the error.

If you’re doing any work with web services, Fiddler is definitly worth considering. Setting it up is fairly straightfoward

By default, Fiddler can update your browser proxy to route through the Fiddler proxy, which is, by default on port 8888, and will capture requests between Chrome/Firefox/Safari/etc, and your web app.

What if you’re debugging locally, and want to intercept .NET services which call other web services? i.e. Web Browser -> Web App -> Service1 -> Service2 (the one you want to debug)? This can be done by setting defaultProxy in web.config. In this case, since we want to debug Service2, we’d set defaultProxy in Service2’s web.config file.

i.e.

<configuration>
 <system.net>
  <defaultProxy>
   <proxy bypassonlocal="false" usesystemdefault="true" />
  </defaultProxy>
 </system.net>
</configuration>

With one of the distributed apps I frequently work with, I was setting, and resetting the web.config defaultProxy fairly frequently. To save some effort, I wrote a script to automate the change.

To use the script, copy it into Powershell ISE, or an new ps1 file, modify the lines at the end of the file to point to the path to the web.config files which you want to update, and execute.

The script is intended to be idempotent and non-destructive, so that it’s safe to execute multiple times.

“Works on my machine” disclaimer: I use this script on my machine every few days days, and it’s been working as intended since creating it a few months ago, however it comes with exactly zero warranty.

If you find any ways which this script can be improved, feel free to get in touch.

# NOTE: Update the paths at the end of this file to point to the web.config files which you want to update

function SetFiddlerAsDefaultProxy($webConfigFullPath)
{
    $doc = (Get-Content $webConfigFullPath) -as [Xml]
 
    $currentSystemNet = $doc.configuration.SelectSingleNode("/configuration/system.net")
    $currentDefaultProxy = $doc.configuration.SelectSingleNode("/configuration/system.net/defaultProxy")
    $currentProxy = $doc.configuration.SelectSingleNode("/configuration/system.net/defaultProxy/proxy")

    $fiddlerProxy = $doc.CreateElement("proxy")
    $fiddlerProxy.SetAttribute("usesystemdefault", "False")
    $fiddlerProxy.SetAttribute("bypassonlocal", "True")
    $fiddlerProxy.SetAttribute("proxyaddress", "http://127.0.0.1:8888")

    if ($currentProxy.usesystemdefault -eq $fiddlerProxy.usesystemdefault `
        -and $currentProxy.bypassonlocal -eq $fiddlerProxy.bypassonlocal `
        -and $currentProxy.proxyaddress -eq $fiddlerProxy.proxyaddress)
    {
        Write-Host "$webConfigFullPath is already set up to use Fiddler as a proxy. Nothing to do"
        Write-Host
    }
    else
    {
        Write-Host "Updating $webConfigFullPath default proxy to use Fiddler"

        if (-not $currentSystemNet)
        {
            Write-Host "Adding system.net section to /configuration"
            $systemNet = $doc.CreateElement("system.net")
            $currentSystemNet = $doc.configuration.AppendChild($systemNet)
        }

        if (-not $currentDefaultProxy) {
            Write-Host "* Adding defaultProxy section to /configuration/system.net"

            $defaultProxy = $doc.CreateElement("defaultProxy")
            $currentDefaultProxy = $currentSystemNet.AppendChild($defaultProxy)
        }

        if (-not $currentProxy) {
            Write-Host "* Adding proxy section to /configuration/system.net/defaultProxy"
            $currentProxy = $currentDefaultProxy.AppendChild($fiddlerProxy)
        }

        $doc.Save($webConfigFullPath)
    }
}

# Update the paths below to point to the web.config files which you want to update
SetFiddlerAsDefaultProxy "C:\dev\webapp\web.config"
SetFiddlerAsDefaultProxy "C:\dev\service1\web.config"
SetFiddlerAsDefaultProxy "C:\dev\service2\web.config"