Locale on Windows Azure
February 17, 2012 3 Comments
Two of the benefits of using the Windows Azure platform is the ability to deploy applications globally and avoiding the need to manage the hardware as well the O/S; however – like everything else in life – this comes at some ‘price’, and one element is control over the environment.
In Windows Azure all instances are created with the en-US locale by default and if your application is deployed outside the US, and you’re not handling this properly, this may cause some confusion.
To demonstrate this I’ve create a simple application using the ASP.net template and added a textbox, a button and a label
In the Page_Load I’ve updated the label with DateTime.Now.ToString() and when I run my application on my UK laptop I get the expected result –
However, deploying and testing this on Windows Azure the result is different – the date shown is in US format (MM/dd/yyyy) rather then the UK format (dd/MM/yyyy) –
The same issue exists when trying to parse a date – entering the date 30/1/2012 into the textbox and clicking on the button which includes the logic –
Label2.Text = DateTime.Now.ToString("MM/dd/yyyy");
result with the correct date displayed in the label when running locally, but an exception when running in Azure (as there’s no month 30, of course)
So – what can one do?
Well – theoretically one can change the locale on the machine, either by using remote desktop (hardly a scalable and reliable approach) or, better yet, by employing a startup task to do this, but this has the potential of confusing the fabric controller and generally speaking – one should not meddle with the O/S unnecessarily.
So – this should be handled at the application level rather than the system level, what’s are the options?
Well – in my simple scenario I could have simply provided the required format in my code – if I had my Page_Load logic as DateTime.Now.ToString(“dd/MM/yyyy”) I would have avoided the different behaviour between environments, similarly I could have provided the format when parsing the date
IFormatProvider cultureInfo = new CultureInfo("en-GB",false); Label2.Text = DateTime.Parse(TextBox2.Text,cultureInfo).ToString();
But this could be quite cumbersome for a real application.
Another option is to set the Culture on the thread of the application –
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
but I would need to do this on every page load, so that might be a bit cumbersome as well (but that’s a good option when you need to set the culture based on the user request, for example)
For a blanket rule option, seems like the web config is the best option – simply add
<globalization culture="en-GB" uiCulture="en-GB" />
to the system.web section of the web config and this culture will be applied to all requests.
A good summary of the options can be found here
Note: turns out there’s a bit more to the story, read my follow up post