vb.net .NET 4.6.2 引入的 System.Web.Globalization 命名空间在运行时与 System.Globalization 发生冲突

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38828248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:00:45  来源:igfitidea点击:

System.Web.Globalization namespace introduced with .NET 4.6.2 conflicts at runtime with System.Globalization

asp.net.netvb.net.net-4.6.2

提问by user247702

After installing the Windows 10 Anniversary Update over the weekend, which includes .NET Framework 4.6.2, some code stopped working. I've gone back to a version of 1 week ago to make sure it's not related to our code.

在周末安装了包含 .NET Framework 4.6.2 的 Windows 10 周年更新后,一些代码停止工作。我已经回到 1 周前的版本,以确保它与我们的代码无关。

At runtime, an error is thrown:

在运行时,抛出一个错误:

error BC30561: 'Globalization' is ambiguous, imported from the namespaces or types 'System.Web, System'.

错误 BC30561:“全球化”不明确,从命名空间或类型“System.Web, System”导入。

Stack trace:

堆栈跟踪:

System.Web.HttpCompileException (0x80004005): C:\path\to\project\MasterPages\SiteMaster.master(71): error BC30561: 'Globalization' is ambiguous, imported from the namespaces or types 'System.Web, System'.
   at System.Web.Compilation.BuildManager.PostProcessFoundBuildResult(BuildResult result, Boolean keyFromVPP, VirtualPath virtualPath)
   at System.Web.Compilation.BuildManager.GetBuildResultFromCacheInternal(String cacheKey, Boolean keyFromVPP, VirtualPath virtualPath, Int64 hashCode, Boolean ensureIsUpToDate)
   at System.Web.Compilation.BuildManager.GetVPathBuildResultFromCacheInternal(VirtualPath virtualPath, Boolean ensureIsUpToDate)
   at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
   at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
   at System.Web.Compilation.BuildManager.GetVPathBuildResult(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean ensureIsUpToDate)
   at System.Web.UI.BaseTemplateParser.GetReferencedType(VirtualPath virtualPath, Boolean allowNoCompile)
   at System.Web.UI.PageParser.ProcessMainDirectiveAttribute(String deviceName, String name, String value, IDictionary parseData)
   at System.Web.UI.TemplateParser.ProcessMainDirective(IDictionary mainDirective)

This is the offending line:

这是违规行:

$.SetLanguage("<%= Globalization.CultureInfo.CurrentUICulture.TwoLetterISOLanguageName %>");

Replacing Globalizationwith System.Globalizationfixes the problem, but Visual Studio suggests that the "name can be simplified", indicating Systemis not necessary.

替换GlobalizationSystem.Globalization修复了问题,但 Visual Studio 建议“名称可以简化”,表明System没有必要。

When setting a breakpoint at the offending line, I can get the same error via the Immediate Window:

在违规行设置断点时,我可以通过立即窗口得到相同的错误:

Globalization.CultureInfo.CurrentUICulture
error BC30560: 'CultureInfo' is ambiguous in the namespace 'System.Globalization'.

If I understand correctly, there is both System.Globalizationand System.Web.Globalization. According to the API diff, a new namespace was introduced, which seems to be causing this issue.

如果我理解正确的话,System.Globalization和都有System.Web.Globalization。根据API diff,引入了一个新的命名空间,这似乎导致了这个问题。

+namespace System.Web.Globalization {
+    public interface IStringLocalizerProvider {
+        string GetLocalizedString(CultureInfo culture, string name, params object[] arguments);
+    }
+    public sealed class ResourceFileStringLocalizerProvider : IStringLocalizerProvider {
+        public const string ResourceFileName = "DataAnnotation.Localization";
+        public ResourceFileStringLocalizerProvider();
+        public string GetLocalizedString(CultureInfo culture, string name, params object[] arguments);
+    }
+    public static class StringLocalizerProviders {
+        public static IStringLocalizerProvider DataAnnotationStringLocalizerProvider { get; set; }
+    }
+}
+namespace System.Web.Globalization {
+    public interface IStringLocalizerProvider {
+        string GetLocalizedString(CultureInfo culture, string name, params object[] arguments);
+    }
+    public sealed class ResourceFileStringLocalizerProvider : IStringLocalizerProvider {
+        public const string ResourceFileName = "DataAnnotation.Localization";
+        public ResourceFileStringLocalizerProvider();
+        public string GetLocalizedString(CultureInfo culture, string name, params object[] arguments);
+    }
+    public static class StringLocalizerProviders {
+        public static IStringLocalizerProvider DataAnnotationStringLocalizerProvider { get; set; }
+    }
+}

Why does this error only appear at runtime? How can I make it fail at compile time?

为什么这个错误只出现在运行时?我怎样才能让它在编译时失败?

回答by drewmerk

Bug Crusher's answer is correct. To address Stijn's comment to the answer, just search your project for "Globalization." and remove every instance of it. I wouldn't use Find + Replace to do this as that may have unintended side effects.

Bug Crusher 的回答是正确的。要解决 Stijn 对答案的评论,只需在您的项目中搜索“全球化”。并删除它的每个实例。我不会使用 Find + Replace 来执行此操作,因为这可能会产生意想不到的副作用。

Then make sure each file you edited has the correct import or using statement on top.

然后确保您编辑的每个文件顶部都有正确的 import 或 using 语句。

VB- Imports System.Globalization

VB-导入系统.全球化

C#- using System.Globalization;

C#- 使用 System.Globalization;

That's the fix VS would've proposed.

这就是 VS 提出的修复方法。

回答by Bug Crusher

We removed the "Globalization." and let Visual Studio propose a fix. We chose "Import System.Globalization" which it added to the file for us.

我们删除了“全球化”。并让 Visual Studio 提出修复方案。我们选择了它为我们添加到文件中的“导入 System.Globalization”。

This got rid of the error and the site works OK.

这消除了错误,网站运行正常。