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

use*_*702 14 .net vb.net asp.net .net-4.6.2

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

在运行时,抛出错误:

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

堆栈跟踪:

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)
Run Code Online (Sandbox Code Playgroud)

这是违规行:

$.SetLanguage("<%= Globalization.CultureInfo.CurrentUICulture.TwoLetterISOLanguageName %>");
Run Code Online (Sandbox Code Playgroud)

更换GlobalizationSystem.Globalization修复问题,但Visual Studio中表明,"名字可以简化",说明System是没有必要的.

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

Globalization.CultureInfo.CurrentUICulture
error BC30560: 'CultureInfo' is ambiguous in the namespace 'System.Globalization'.
Run Code Online (Sandbox Code Playgroud)

如果我理解正确,有System.GlobalizationSystem.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; }
+    }
+}
Run Code Online (Sandbox Code Playgroud)

为什么此错误仅出现在运行时?如何在编译时使其失败?

dre*_*erk 8

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

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

VB-进口System.Globalization

C# - 使用System.Globalization;

这就是VS提出的解决方案.


小智 3

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

这消除了错误,并且网站工作正常。