While developing a Windows Phone 7 application I ran into a tricky problem. The RESTful Web Service that was to be called by the application was still in development and therefore not available on the internet. The service was hosted on an intranet server. The Windows Phone emulator was able to connect to the service but I couldn’t get the Phone device to connect to it.
The device was connected to the network using a USB cable and I disabled its Wi-Fi connection to make sure all network traffic went through my PC which is part of the windows domain. Using the phone’s browser to browse to the service I got 502 HTTP results: Bad Gateway
At the same time my PC was able to browse to the same address without a hitch. So something in between the phone and my PC was messing things up. that something in between was Zune. Zune is the program that allows the phone device to communicate with the PC and it directs network communication. One of the things it does is detecting the proxy servers on the network and that was what went wrong. The name of the server had a format (dev.www.ourwebsite.net) that triggered Zune to use the Proxy Server. Our proxy server only serves internet addresses so the request for the intranet server was lost.
These are the solutions I tried and that failed me:
- Get the server renamed or aliased – no go. Renaming might cause problems for others and IIS refused an extra host name/alias
- Configure Zune better – no go. Except for some shady registry settings I can not find an official way of configuring the behavior of Zune
- Use Fiddler to intercept the traffic of the phone and redirect the calls to a fake address to the intranet server – no go. I couldn’t get Fiddler to intercept the traffic. I even tried using the WPAD server fiddler extension. I’d love to get this to work because it can be very useful for other scenarios such as monitoring bandwidth
- Writing a WCF 4 Router – no go. A WCF Router can only route WCF WS services by using SOAP headers.
This is what worked:
I installed Application Request Routing for IIS 7 on my PC using the Microsoft Web Platform Installer
In the IIS Manager I selected my machine (top node on the left) and clicked Application RequestRouting Cache:
On the left I selected Server Proxy Settings:
And then checked Enable Proxy:
Then I added a Virtual Directory to the Default Web Site, I named it DEVNET, and selected it:
The selected URL Rewrite:
And clicked Add Rule(s):
Selected Reverse Proxy:
Entered the server name (dev.www.ourwebsite.net) and checked Rewite the domain names of the links in HTTP responses:
An Inbound and Outbound rule had been added to the URL Rewrite configuration.
I opened the Inbound rule and added the path on the web server to the Rewrite URL:
And I added the same path to the Outbound pattern:
And there you go! The Phone app will use URLs such as http://myPC/en/categories and these requests will be routed to http://dev.www.ourwebsite.net/ISTraining.External/trainingservice/en/categories
This way it is also easy to setup a mocking services for the phone app. Both the emulator and the device use the same URLs now which makes development much easier.