Skip to content

Commit

Permalink
Better error message in ActivatorUtilities (#43762)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisvergeer committed Jan 7, 2021
1 parent 0941940 commit 35e9354
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static object CreateInstance(

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);
}

Expand Down Expand Up @@ -212,7 +212,7 @@ private static void FindApplicableConstructor(
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public void TypeActivatorRequiresPublicConstructor(CreateInstanceFunc createFunc
{
// 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>(() =>
Expand All @@ -165,7 +165,7 @@ public void TypeActivatorRequiresAllArgumentsCanBeAccepted(CreateInstanceFunc cr
{
// 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);
Expand Down Expand Up @@ -373,7 +373,7 @@ public void CreateInstance_WithAbstractTypeAndPublicConstructor_ThrowsCorrectExc
{
// 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);
}

Expand Down

0 comments on commit 35e9354

Please sign in to comment.