From: Joris Vergeer Date: Thu, 7 Jan 2021 16:48:43 +0000 (+0100) Subject: Better error message in ActivatorUtilities (#43762) X-Git-Tag: submit/tizen/20210909.063632~3896 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=35e935441c4c88611b6635cbf4a0f482cbad863c;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Better error message in ActivatorUtilities (#43762) --- diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ActivatorUtilities.cs b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ActivatorUtilities.cs index 92be149..94bbfdb 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ActivatorUtilities.cs +++ b/src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ActivatorUtilities.cs @@ -69,7 +69,7 @@ namespace Microsoft.Extensions.DependencyInjection if (bestLength == -1) { - string? message = $"A suitable constructor for type '{instanceType}' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor."; + string? message = $"A suitable constructor for type '{instanceType}' could not be located. Ensure the type is concrete and all parameters of a public constructor are either registered as services or passed as arguments. Also ensure no extraneous arguments are provided."; throw new InvalidOperationException(message); } @@ -212,7 +212,7 @@ namespace Microsoft.Extensions.DependencyInjection if (!TryFindPreferredConstructor(instanceType, argumentTypes, ref constructorInfo, ref parameterMap) && !TryFindMatchingConstructor(instanceType, argumentTypes, ref constructorInfo, ref parameterMap)) { - string? message = $"A suitable constructor for type '{instanceType}' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor."; + string? message = $"A suitable constructor for type '{instanceType}' could not be located. Ensure the type is concrete and all parameters of a public constructor are either registered as services or passed as arguments. Also ensure no extraneous arguments are provided."; throw new InvalidOperationException(message); } diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.Specification.Tests/ActivatorUtilitiesTests.cs b/src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.Specification.Tests/ActivatorUtilitiesTests.cs index 8bfd657..73ce408 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.Specification.Tests/ActivatorUtilitiesTests.cs +++ b/src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.Specification.Tests/ActivatorUtilitiesTests.cs @@ -150,7 +150,7 @@ namespace Microsoft.Extensions.DependencyInjection.Specification { // Arrange var expectedMessage = $"A suitable constructor for type '{type}' could not be located. " + - "Ensure the type is concrete and services are registered for all parameters of a public constructor."; + "Ensure the type is concrete and all parameters of a public constructor are either registered as services or passed as arguments. Also ensure no extraneous arguments are provided."; // Act and Assert var ex = Assert.Throws(() => @@ -165,7 +165,7 @@ namespace Microsoft.Extensions.DependencyInjection.Specification { // Arrange var expectedMessage = $"A suitable constructor for type '{typeof(AnotherClassAcceptingData).FullName}' could not be located. " + - "Ensure the type is concrete and services are registered for all parameters of a public constructor."; + "Ensure the type is concrete and all parameters of a public constructor are either registered as services or passed as arguments. Also ensure no extraneous arguments are provided."; var serviceCollection = new TestServiceCollection() .AddTransient(); var serviceProvider = CreateServiceProvider(serviceCollection); @@ -373,7 +373,7 @@ namespace Microsoft.Extensions.DependencyInjection.Specification { // Act & Assert var ex = Assert.Throws(() => ActivatorUtilities.CreateInstance(default(IServiceProvider), typeof(AbstractFoo))); - var msg = "A suitable constructor for type 'Microsoft.Extensions.DependencyInjection.Specification.DependencyInjectionSpecificationTests+AbstractFoo' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor."; + var msg = "A suitable constructor for type 'Microsoft.Extensions.DependencyInjection.Specification.DependencyInjectionSpecificationTests+AbstractFoo' could not be located. Ensure the type is concrete and all parameters of a public constructor are either registered as services or passed as arguments. Also ensure no extraneous arguments are provided."; Assert.Equal(msg, ex.Message); }