Thursday, December 22, 2016

Redirecting cloud service traffic to load host and port

When you are developing cloud services, sometimes you want to debug the service which is hosted outside of your dev box.
The most common case is that there is a web portal which talks to a web service. The web service is not behaving as expected and you want to debug it.

The easiest step is to add an entry in host file (%SystemRoot%\System32\drivers\etc\hosts)
127.0.0.1              mywebservice.com

Now, when you open the web portal on your dev box, the web service hosted on your dev box will be called and you can debug it by putting breakpoints.

Now comes the interesting part. What if the web service on your dev box is running on a specific port say https://localhost:44310/
Just adding a host entry will not work since you want to redirect the traffic to your host as well as port.

Fiddler comes to rescue here which can be downloaded from http://www.telerik.com/fiddler
You need to do the following steps:

1. No need to change the host file (%SystemRoot%\System32\drivers\etc\hosts)
2. Open Fiddler
3. Go to Tools->Host and add the below entry
 localhost              mywebservice.com
4. Go to Rule->Customize Rules and add the below snippet in static function OnBeforeRequest(oSession: Session)
if (oSession.HostnameIs("mywebservice.com"))
{
            oSession.host="localhost:44310";
}
5.Restart Fiddler

That’s it, now when you open the web portal on your dev box, the web service hosted on your dev box will be called and you can debug it by putting breakpoints.