Modified exception message for duplicate keys exception in CandidateResolver

Addresses #5289
This commit is contained in:
Jass Bagga 2016-11-07 11:16:11 -08:00 committed by GitHub
parent 304000095a
commit 9caa688a30
4 changed files with 57 additions and 2 deletions

View File

@ -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<RuntimeLibrary> dependencies, ISet<string> referenceAssemblies)
{
_dependencies = dependencies
.ToDictionary(d => d.Name, d => CreateDependency(d, referenceAssemblies), StringComparer.OrdinalIgnoreCase);
var dependenciesWithNoDuplicates = new Dictionary<string, Dependency>(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<string> referenceAssemblies)

View File

@ -1354,6 +1354,22 @@ namespace Microsoft.AspNetCore.Mvc.Core
return string.Format(CultureInfo.CurrentCulture, GetString("VaryByQueryKeys_Requires_ResponseCachingMiddleware"), p0);
}
/// <summary>
/// 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.
/// </summary>
internal static string CandidateResolver_DifferentCasedReference
{
get { return GetString("CandidateResolver_DifferentCasedReference"); }
}
/// <summary>
/// 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.
/// </summary>
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);

View File

@ -379,4 +379,8 @@
<data name="VaryByQueryKeys_Requires_ResponseCachingMiddleware" xml:space="preserve">
<value>'{0}' requires the response cache middleware.</value>
</data>
<data name="CandidateResolver_DifferentCasedReference" xml:space="preserve">
<value>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.</value>
<comment>{0} is the dependency name.</comment>
</data>
</root>

View File

@ -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<RuntimeFallbacks>());
// Act
var exception = Assert.Throws<InvalidOperationException>(() => 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()
{