From 9caa688a30b76f148688f13b759d666f8d75052e Mon Sep 17 00:00:00 2001 From: Jass Bagga Date: Mon, 7 Nov 2016 11:16:11 -0800 Subject: [PATCH] Modified exception message for duplicate keys exception in CandidateResolver Addresses #5289 --- .../DefaultAssemblyPartDiscoveryProvider.cs | 14 +++++++++-- .../Properties/Resources.Designer.cs | 16 ++++++++++++ .../Resources.resx | 4 +++ ...efaultAssemblyPartDiscoveryProviderTest.cs | 25 +++++++++++++++++++ 4 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Internal/DefaultAssemblyPartDiscoveryProvider.cs b/src/Microsoft.AspNetCore.Mvc.Core/Internal/DefaultAssemblyPartDiscoveryProvider.cs index d5db9a861d..e6df5bc816 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/Internal/DefaultAssemblyPartDiscoveryProvider.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/Internal/DefaultAssemblyPartDiscoveryProvider.cs @@ -7,6 +7,7 @@ using System.Diagnostics; using System.Linq; using System.Reflection; using Microsoft.AspNetCore.Mvc.ApplicationParts; +using Microsoft.AspNetCore.Mvc.Core; using Microsoft.Extensions.DependencyModel; namespace Microsoft.AspNetCore.Mvc.Internal @@ -73,8 +74,17 @@ namespace Microsoft.AspNetCore.Mvc.Internal public CandidateResolver(IReadOnlyList dependencies, ISet referenceAssemblies) { - _dependencies = dependencies - .ToDictionary(d => d.Name, d => CreateDependency(d, referenceAssemblies), StringComparer.OrdinalIgnoreCase); + var dependenciesWithNoDuplicates = new Dictionary(StringComparer.OrdinalIgnoreCase); + foreach (var dependency in dependencies) + { + if (dependenciesWithNoDuplicates.ContainsKey(dependency.Name)) + { + throw new InvalidOperationException(Resources.FormatCandidateResolver_DifferentCasedReference(dependency.Name)); + } + dependenciesWithNoDuplicates.Add(dependency.Name, CreateDependency(dependency, referenceAssemblies)); + } + + _dependencies = dependenciesWithNoDuplicates; } private Dependency CreateDependency(RuntimeLibrary library, ISet referenceAssemblies) diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Properties/Resources.Designer.cs b/src/Microsoft.AspNetCore.Mvc.Core/Properties/Resources.Designer.cs index ab08d318b2..bef5912b9a 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/Properties/Resources.Designer.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/Properties/Resources.Designer.cs @@ -1354,6 +1354,22 @@ namespace Microsoft.AspNetCore.Mvc.Core return string.Format(CultureInfo.CurrentCulture, GetString("VaryByQueryKeys_Requires_ResponseCachingMiddleware"), p0); } + /// + /// A duplicate entry for library reference {0} was found. Please check that all package references in all projects use the same casing for the same package references. + /// + internal static string CandidateResolver_DifferentCasedReference + { + get { return GetString("CandidateResolver_DifferentCasedReference"); } + } + + /// + /// A duplicate entry for library reference {0} was found. Please check that all package references in all projects use the same casing for the same package references. + /// + internal static string FormatCandidateResolver_DifferentCasedReference(object p0) + { + return string.Format(CultureInfo.CurrentCulture, GetString("CandidateResolver_DifferentCasedReference"), p0); + } + private static string GetString(string name, params string[] formatterNames) { var value = _resourceManager.GetString(name); diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Resources.resx b/src/Microsoft.AspNetCore.Mvc.Core/Resources.resx index e5d07f4d0f..0575c33b57 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/Resources.resx +++ b/src/Microsoft.AspNetCore.Mvc.Core/Resources.resx @@ -379,4 +379,8 @@ '{0}' requires the response cache middleware. + + A duplicate entry for library reference {0} was found. Please check that all package references in all projects use the same casing for the same package references. + {0} is the dependency name. + \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.Mvc.Core.Test/Internal/DefaultAssemblyPartDiscoveryProviderTest.cs b/test/Microsoft.AspNetCore.Mvc.Core.Test/Internal/DefaultAssemblyPartDiscoveryProviderTest.cs index abc3a35246..ee090ddb0e 100644 --- a/test/Microsoft.AspNetCore.Mvc.Core.Test/Internal/DefaultAssemblyPartDiscoveryProviderTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Core.Test/Internal/DefaultAssemblyPartDiscoveryProviderTest.cs @@ -14,6 +14,31 @@ namespace Microsoft.AspNetCore.Mvc.Internal private static readonly Assembly CurrentAssembly = typeof(DefaultAssemblyPartDiscoveryProviderTests).GetTypeInfo().Assembly; + [Fact] + public void CandidateResolver_ThrowsIfDependencyContextContainsDuplicateRuntimeLibraryNames() + { + // Arrange + var upperCaseLibrary = "Microsoft.AspNetCore.Mvc"; + var mixedCaseLibrary = "microsoft.aspNetCore.mvc"; + + var dependencyContext = new DependencyContext( + new TargetInfo("framework", "runtime", "signature", isPortable: true), + CompilationOptions.Default, + new CompilationLibrary[0], + new[] + { + GetLibrary(mixedCaseLibrary), + GetLibrary(upperCaseLibrary), + }, + Enumerable.Empty()); + + // Act + var exception = Assert.Throws(() => DefaultAssemblyPartDiscoveryProvider.GetCandidateLibraries(dependencyContext)); + + // Assert + Assert.Equal($"A duplicate entry for library reference {upperCaseLibrary} was found. Please check that all package references in all projects use the same casing for the same package references.", exception.Message); + } + [Fact] public void GetCandidateLibraries_IgnoresMvcAssemblies() {