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

14

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.

At runtime, an error is thrown:

error BC30561: 'Globalization' is ambiguous, imported from the namespaces or types '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 Globalization with System.Globalization fixes the problem, but Visual Studio suggests that the "name can be simplified", indicating System is not necessary.

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.Globalization and System.Web.Globalization. According to the API diff, a new namespace was introduced, which seems to be causing this issue.

+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?

asp.net
.net
vb.net
.net-4.6.2
asked on Stack Overflow Aug 8, 2016 by user247702 • edited Aug 8, 2016 by user247702

2 Answers

8

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.

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

VB- Imports System.Globalization

C#- using System.Globalization;

That's the fix VS would've proposed.

answered on Stack Overflow Sep 23, 2016 by drewmerk
3

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

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

answered on Stack Overflow Aug 9, 2016 by Bug Crusher

User contributions licensed under CC BY-SA 3.0