Windows Azure - Accessing the Request.Url Property!

So I've blogged previously about a known issue regarding port translation/mapping in Windows Azure, and today (well, today and a few weeks ago) I came across an interesting side effect of this issue when accessing the Request.Url property within a Windows Azure web role.

Now, anytime you access the HttpContext.Request.Url property (or any derivation of that), you will be unknowingly getting some extra info you may not want.

For example (and as it happens, the piece of code at the center of today's investigation), if you are building a new URL in your Windows Azure application by calling:

string RootUri = HttpContext.Current.Request.Url.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);

You're going to get something along the lines of:

Scheme: Http


Port: 20000 <-- Aha! (I added the <-- Aha! for dramatic effect)

This is going to cause your app issues if you are then trying to use that in a dynamic link or web request.

The solution is simple, anytime you call something that returns a Request.Url object, make sure you ensure any calls you make on that Url object do not explicitly return the port (this gets tricky when doing local testing where you need the port, so you'll need to manage that). Construct your new URL with only host or path details, otherwise you'll make mischief for yourself!

Enjoy :)

Technorati Tags: Windows Azure,Request.Url