Friday 24 June 2016

Redirecting VS requests to services using fiddler

If you want to look at the traffic with Fiddler, you probably want to go the route of changing the machine.config file so that all .NET applications will send traffic through Fiddler. This helps you ensure that you capture data from processes running in services, etc. Also this is great way to save time when working with multiple services.

Expose call to fiddler 


At first we have to modify web.config by adding following.

<system.net>
      <defaultProxy
                 enabled = "true"
                 useDefaultCredentials = "true">
        <proxy autoDetect="false" bypassonlocal="false" proxyaddress="http://127.0.0.1:8888" usesystemdefault="false" />
      </defaultProxy>
    </system.net>
After this we can see requests from Visual Studio in our fiddler


Now we need to rewrite the requests

For this section we need to download  following plugin

http://www.telerik.com/download/fiddler/fiddlerscript-editor

In fiddler window is FiddlerScript tab usually on right hand side (unless you have changed it)


You can scroll or use search to find: "OnBeforeRequest". I have highlighted it for you on picture above.

Insert following snippet and modify it to what you want to use.


 // fiddler configuration to rewrite call to local instance of services
 if (oSession.HostnameIs("testservice.com")) {
         oSession.hostname = "local.testservice.com";          
 } 
           
Lets describe this:
first line  

 if (oSession.HostnameIs("testservice.com")) {
searches requests based on name and needs to be same


Second line     
this is the replacement line
         oSession.hostname = "local.testservice.com";    

Now if you run application with internal call, it will be redirected to local instance.

Advanced configuration 


You can apply the change in machine.config if you want to apply it to all applications as follows.

Open machine.config in the folder C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config. Note that if you are debugging a 64bit service (like ASP.NET) you will want to look in the Framework64 folder instead of the Framework folder. Similarly, if you are using a .NET version prior to 4.0, you will need to adjust the version part of the path.

Add the following XML block as a peer to the existing system.net element, replacing any existing defaultProxy element if present as follows:

<system.net>
      <defaultProxy
                 enabled = "true"
                 useDefaultCredentials = "true">
        <proxy autoDetect="false" bypassonlocal="false" proxyaddress="http://127.0.0.1:8888" usesystemdefault="false" />
      </defaultProxy>
    </system.net>