fre_der
fre_der

Reputation: 83

Why does "new CultureInfo("xx")" throw an exception in one environment but not in another?

My Asp.Net MVC application targets .Net Framework 4.6.1

In my dev environment, this works:

var x = new CultureInfo("xx")

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.8.4210.0 (I got this info from a random error page, elsewhere in the same MVC application, it's mentioned at the bottom)

IIS 10.0.18362.650 on Windows 10 build 18363.1016 (info gotten from IIS manager - help - about)

On a hosted staging (and live) environment, the same code throws an exception

[CultureNotFoundException: Culture is not supported.
Parameter name: name
xx is an invalid culture identifier.]
   System.Globalization.CultureInfo..ctor(String name, Boolean useUserOverride) +230

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.8.3928.0 IIS 8.5.9600.16384 on Windows Server 2012 R2

I'm aware that "xx" isn't a valid culture (I just used it to compare resources files in different languages, and needed a non existing culture). But what I want to know is why this fails on one environment and works on another. The asp.net versions are (almost) the same.

Upvotes: 2

Views: 679

Answers (1)

Matteo Umili
Matteo Umili

Reputation: 4017

What is happen is when you create a culture with name like "aaa" which is is not one of the names carried by Windows, at that time Windows will try to create it for us and try to fill the culture properties as much it can by matching the language part and country part. for example, if I try to create ja-US which I think not exist, Windows will try to fake the culture by getting the language information from the Japanese language and will get the country information from US and combine it into a new culture. now when passing "aaa" which not matching any language/country, Windows will just use Invariant culture properties for that. And that is what you are seeing.

This is the behavior defined by Windows starting Windows 10 and not really the .NET. the framework just calling the OS for getting the needed information. the code of CreateSpecificCulture is just calling the OS at the end.

Source

Upvotes: 2

Related Questions