.NET中de-CH文化数字分组分隔符本地与Azure环境差异原因
Great question—this discrepancy boils down to where .NET pulls its globalization/culture data from in each environment, and it's a common gotcha with cross-environment .NET applications. Let's break it down:
1. Local environments rely on OS-level regional settings
On your local Windows machine, both .NET Framework 4.7.2 and .NET Core 3.1 pull culture data directly from the Windows operating system's regional settings. For the de-CH (Swiss German) culture, Windows natively uses the Unicode character U+2019 (right single quotation mark—this is the "apostrophe-like" character you're seeing) as the number group separator, which aligns with Swiss German formatting standards.
When you run your test code locally:
using System; using System.Linq; using System.Globalization; Console.WriteLine(((int)(CultureInfo.GetCultureInfo("de-CH").NumberFormat.NumberGroupSeparator.Single())).ToString("X4"));
It's fetching that OS-defined character, hence the output 2019.
2. Azure environments use .NET's built-in globalization data
Azure services (like App Service, Azure Functions, or the Try .NET platform) are designed for consistency across underlying operating systems (they might run on Windows Server or Linux depending on the plan). To avoid OS-specific variations, they default to using .NET Runtime's built-in globalization dataset instead of the host OS's regional settings.
In this built-in dataset, the de-CH culture's number group separator is set to the ASCII apostrophe U+0027—this is why your test code outputs 0027 in Azure.
3. Why parsing fails locally but works in Azure
When you try to parse the string 4'200.000 (with the ASCII U+0027 apostrophe) locally using the default de-CH culture, the parser expects the U+2019 character as the group separator. Since they don't match, parsing fails. In Azure, the default separator is U+0027, so the string matches perfectly and parsing succeeds.
How to fix the inconsistency
To make your application behave the same across local and Azure environments, you have a couple of options:
Manually set the separator: Clone the
de-CHculture and explicitly define the number group separator to the character you need (eitherU+0027orU+2019), instead of relying on environment defaults.var chCulture = (CultureInfo)CultureInfo.GetCultureInfo("de-CH").Clone(); chCulture.NumberFormat.NumberGroupSeparator = "'"; // Use ASCII apostrophe // Or use the right single quote: chCulture.NumberFormat.NumberGroupSeparator = "\u2019"; // Use this modified culture for all parsing/formatting operationsForce .NET to use OS-level data in Azure (for .NET Core 3.1+): If you want Azure to match your local Windows behavior, you can enable the NLS globalization mode. Add this to your application startup code (e.g.,
Program.csfor Azure Functions):AppContext.SetSwitch("System.Globalization.UseNls", true);This tells .NET to use the OS's native globalization data instead of its built-in set, aligning Azure's behavior with your local Windows machine.
内容的提问来源于stack exchange,提问作者Josh Gallagher

