Azure网络应用程序突然不再支持文化

时间:2017-01-25 12:25:08

标签: c# asp.net asp.net-mvc azure azure-web-sites

我们的Azure网络应用程序突然发现有关不受支持的文化的错误。我们会在首页上显示要显示的国家/地区列表,但这会突然出错。其他各种网络应用程序也使用相同的代码,但它们没有问题。

以下代码给出了一个问题。

 private List<SelectListItem> Countries()
        {
            RegionInfo country = new RegionInfo(new CultureInfo("nl-BE", false).LCID);
            List<SelectListItem> countryNames = new List<SelectListItem>();
        foreach (CultureInfo cul in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
        {
            country = new RegionInfo(new CultureInfo(cul.Name, false).LCID);
            countryNames.Add(new SelectListItem() { Text = country.DisplayName, Value = country.DisplayName });
        }

        return countryNames.GroupBy(x => x.Text).Select(x => x.FirstOrDefault()).ToList<SelectListItem>().OrderBy(x => x.Text).ToList();
    }

我在for-each中放置了一个try-catch,因此我可以查明出错的文化。以下文化突然返回错误:

<errors>
<LCID>4096</LCID>
<Name>ar-001</Name>
</errors>
<errors>
<LCID>4096</LCID>
<Name>el-CY</Name>
</errors>
<errors>
<LCID>4096</LCID>
<Name>en-BB</Name>
</errors>
<errors>
<LCID>4096</LCID>
<Name>en-BS</Name>
</errors>
<errors>
<LCID>4096</LCID>
<Name>en-HK</Name>
</errors>
<errors>
<LCID>4096</LCID>
<Name>en-NL</Name>
</errors>
<errors>
<LCID>4096</LCID>
<Name>en-SE</Name>
</errors>
<errors>
<LCID>4096</LCID>
<Name>es-419</Name>
</errors>

有人可以帮我解决这个问题吗?我似乎无法理解为什么这个网络应用突然出现这些错误。

2 个答案:

答案 0 :(得分:16)

Windows中的几乎所有新语言环境都没有分配显式LCID - 因为数百个国家/地区的数千种语言没有足够的“空间”问题。他们都被分配了0x1000。

在这种情况下,我认为更改LCID名称可能适合您:

country = new RegionInfo(new CultureInfo(cul.Name, false).LCID);

只使用文化名称:

country = new RegionInfo(cul.Name);

当然,LCID的任何其他用途也需要识别文化名称。

我们实际上建议使用完整的文化名称构造RegionInfo,因为它比区域名称更明确。 RegionInfo有一些“依赖”语言的属性,例如DisplayName。例如,es-US和en-US为“美国”提供西班牙语或英语字符串。

希望有所帮助,

-Shawn

答案 1 :(得分:4)

我确认这些是刚刚在Azure App Service中添加的新文化,显然它是以创建RegionInfo失败的方式完成的。最简单的责任是:

var culture = new CultureInfo("en-HK", false).LCID;
var region = new RegionInfo(culture);

这失败了:

Unhandled Exception: System.Globalization.CultureNotFoundException: Culture is not supported. Parameter name: culture 4096 (0x1000) is an invalid culture identifier. at System.Globalization.CultureData.GetCultureData(Int32 culture, Boolean bUseUserOverride) at System.Globalization.RegionInfo..ctor(Int32 culture)

我们正在进一步调查。与此同时,建议的解决方法是捕获异常并忽略失败的文化。