Better error message in ActivatorUtilities (#43762)
authorJoris Vergeer <jorisvergeer1992@gmail.com>
Thu, 7 Jan 2021 16:48:43 +0000 (17:48 +0100)
committerGitHub <noreply@github.com>
Thu, 7 Jan 2021 16:48:43 +0000 (10:48 -0600)
src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ActivatorUtilities.cs
src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.Specification.Tests/ActivatorUtilitiesTests.cs

index 92be149..94bbfdb 100644 (file)
@@ -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);
             }
 
index 8bfd657..73ce408 100644 (file)
@@ -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<InvalidOperationException>(() =>
@@ -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<IFakeService, FakeService>();
             var serviceProvider = CreateServiceProvider(serviceCollection);
@@ -373,7 +373,7 @@ namespace Microsoft.Extensions.DependencyInjection.Specification
         {
             // Act & Assert
             var ex = Assert.Throws<InvalidOperationException>(() => 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);
         }