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

user247702 picture user247702 · Aug 8, 2016 · Viewed 11.1k times · Source

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?

Answer

drewmerk picture drewmerk · Sep 24, 2016

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.