Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 591
user247702 Avatar asked Aug 08 '16 11:08

user247702


People also ask

Which namespace is used for globalization?

The answer is A. The System. Globalization namespace contains classes that define culture-related information, including the language, the country/region, the calendars in use, the format patterns for dates, currency, and numbers, and the sort order for strings.

Which of the following namespaces in C# is used for globalization?

In C#, global namespace is the root namespace.

Which of the following namespace provides culture specific information?

NumberFormatInfo Class (System. Globalization) Provides culture-specific information for formatting and parsing numeric values.


2 Answers

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.

like image 174
drewmerk Avatar answered Sep 22 '22 03:09

drewmerk


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.

like image 23
Bug Crusher Avatar answered Sep 22 '22 03:09

Bug Crusher