[英]Azure App Service seems to have NLS enabled in .NET 5 mode
After spending countless of hours on getting to the core of a bug, I eventually boiled down a problem to the use of string.Compare
with StringComparison.InvariantCultureIgnoreCase
in .NET 5.在花了无数个小时来了解错误的核心之后,我最终将问题
string.Compare
为在 .NET 5 中使用string.Compare
和StringComparison.InvariantCultureIgnoreCase
。
Consider the following two dotnetfiddles:考虑以下两个 dotnetfiddle:
When running the .NET 4.7.2 you get -1 as a result, when running .NET 5 you get 1 as a result.运行 .NET 4.7.2 时,结果为 -1,运行 .NET 5 时,结果为 1。
After some browsing, this led to the following notice:经过一番浏览,这导致了以下通知:
So, going by this, a result of -1 is the NLS version, whereas the .NET 5 result of 1 is the ICU version.所以,按照这个,-1 的结果是 NLS 版本,而 1 的 .NET 5 结果是 ICU 版本。
However, when I spin up an Azure App Service in .NET 5 mode, the result of the above code in a Razor page is -1, AKA: the NLS version.但是,当我在 .NET 5 模式下启动 Azure 应用服务时,上述代码在 Razor 页面中的结果是 -1,也就是:NLS 版本。
This can cause all kinds of weird issues, because two different systems lead to unexpected results.这可能会导致各种奇怪的问题,因为两个不同的系统会导致意想不到的结果。
When I add add the following to my project-file, as mentioned in the last article, my local environment also outputs a -1.当我将以下内容添加到我的项目文件中时,如上一篇文章所述,我的本地环境也输出 -1。
<ItemGroup>
<RuntimeHostConfigurationOption Include="System.Globalization.UseNls" Value="true" />
</ItemGroup>
No matter what kind of configuration I use in Azure, it will always keep outputting -1.无论我在 Azure 中使用什么样的配置,它都会一直输出 -1。
Long story, something is up on Azure.说来话长,Azure 出了点问题。 As per documentation, my Windows version is new enough to have ICU enabled.
根据文档,我的 Windows 版本足够新,可以启用 ICU。 Looks like the Azure App Service is either using a forced NLS mode, or is running some ICU version my local machine doesn't have.
看起来 Azure 应用服务要么使用强制 NLS 模式,要么正在运行我的本地机器没有的某些 ICU 版本。
Anyone know how I can figure out which ICU version (if any) Azure is using, so I can use the suggestion from the documentation to use a AppLocalIcu?任何人都知道我如何确定 Azure 正在使用哪个 ICU 版本(如果有),以便我可以使用文档中的建议来使用 AppLocalIcu? Otherwise, if something is clearly on the side of Azure, then my question is what the best location would be to report this?
否则,如果 Azure 方面显然有某些事情,那么我的问题是报告此内容的最佳位置是什么?
I think you can use this method to enable the App-local ICU in the Azure app service.我觉得可以用这个方法在Azure应用服务中启用App-local ICU。
If your web app is a framework-dependent application, you can consume ICU via a NuGet package.如果您的 Web 应用程序是依赖于框架的应用程序,则可以通过 NuGet 包使用 ICU。
<RuntimeHostConfigurationOption Include="System.Globalization.AppLocalIcu" Value="<suffix>:<version> or <version>" />
in the section <ItemGroup>
.<ItemGroup>
部分添加<RuntimeHostConfigurationOption Include="System.Globalization.AppLocalIcu" Value="<suffix>:<version> or <version>" />
<ItemGroup>
。 Or you can add an app setting DOTNET_SYSTEM_GLOBALIZATION_APPLOCALICU
with the value <suffix>:<version>
or <version>
.DOTNET_SYSTEM_GLOBALIZATION_APPLOCALICU
,其值为<suffix>:<version>
或<version>
。 Then you can use the special version of ICU in the Azure App Service as you want.然后,您可以根据需要在 Azure 应用服务中使用特殊版本的 ICU。
Someone at the Azure App Service team dove into this: Azure 应用服务团队的某个人对此深有体会:
Major Minor Build Revision
----- ----- ----- --------
10 0 14393 0
So to answer my own question: Azure App Services are indeed using NLS by default.所以回答我自己的问题:Azure 应用服务确实在默认情况下使用 NLS。 This is not a bug!
这不是错误!
By including the following in your project-file, ICU will be forced:通过在您的项目文件中包含以下内容,ICU 将被强制执行:
<ItemGroup Condition="'$(OS)' == 'Windows_NT'">
<PackageReference Include="Microsoft.ICU.ICU4C.Runtime" Version="68.2.0.9" />
<RuntimeHostConfigurationOption Include="System.Globalization.AppLocalIcu" Value="68.2" />
</ItemGroup>
This is in line with the solution that @Crazy Crab mentioned, thanks!这与@Crazy Crab 提到的解决方案一致,谢谢!
Also see https://www.nuget.org/packages/Microsoft.ICU.ICU4C.Runtime for the latest version (68.2.0.9 at the time of writing).另请参阅https://www.nuget.org/packages/Microsoft.ICU.ICU4C.Runtime了解最新版本(撰写本文时为 68.2.0.9)。
I'm going to accept my own answer, as I feel it gives a better answer to the "Why is this happening" question, rather than just fixing it.我将接受我自己的答案,因为我觉得它可以更好地回答“为什么会发生这种情况”的问题,而不仅仅是修复它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.