From 35e935441c4c88611b6635cbf4a0f482cbad863c Mon Sep 17 00:00:00 2001 From: Joris Vergeer Date: Thu, 7 Jan 2021 17:48:43 +0100 Subject: [PATCH] Better error message in ActivatorUtilities (#43762) --- .../src/ActivatorUtilities.cs | 4 ++-- .../tests/DI.Specification.Tests/ActivatorUtilitiesTests.cs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) 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); } -- 2.7.4