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?

user247702
  • 23,641
  • 15
  • 110
  • 157
  • Seems the offending line is inline code in aspx page which is compiled during the first request. You can use aspnet_compiler.exe to precompile your web app and you should see the error. – mattfei Aug 09 '16 at 06:01
  • Same issue I am getting. The code was working fine but after reinstalling VS and all other thing, it started giving same error. but I got after instllation of Framework 4.5.2. Did you find the solution? – par Sep 08 '16 at 05:33
  • @par The solution is to fully qualify the type and ignore Visual Studio when it says the name can be simplified. I assume it's also not a problem if your project targets 4.6.2 but I haven't tried yet. – user247702 Sep 08 '16 at 07:46
  • @Stijn, thanks. My project targets 4.5.1 – par Sep 08 '16 at 08:01
  • Had the same problem with a web site(.net 4.0) – L C Nov 11 '16 at 06:53

2 Answers2

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.

drewmerk
  • 332
  • 4
  • 10
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.