-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better error message in ActivatorUtilities #43762
Conversation
Tagging subscribers to this area: @eerhardt, @maryamariyan Issue DetailsThe current error message gave me no pointers on what was/went wrong when I had an extra parameter to Adding to the error message that all parameters have to be consumed would have saved me some time, and I hope that this change will save some time for a future developer. Example that gave me unclear instructions: private interface IService
{
string Name { get; }
}
private class Service : IService
{
public string Name { get; set; }
}
private class MyClass
{
private readonly IService serviceA;
private readonly IService serviceB;
public MyClass(IService serviceA, IService serviceB)
{
this.serviceA = serviceA;
this.serviceB = serviceB;
}
public void Print()
{
Console.WriteLine(serviceA.Name);
Console.WriteLine(serviceB.Name);
}
}
static void Main(string[] args)
{
var services = new ServiceCollection();
services.AddSingleton<IService>(p => new Service { Name = "Injected" });
var provider = services.BuildServiceProvider();
var special = new Service { Name = "Special" };
// The extra "Unused" string is not consumed by MyClass
ActivatorUtilities.CreateInstance<MyClass>(provider, special, "Unused");
// Throws: System.InvalidOperationException: 'A suitable constructor for type 'TestProject.Program+MyClass' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.'
}
|
src/libraries/Common/src/Extensions/ActivatorUtilities/ActivatorUtilities.cs
Outdated
Show resolved
Hide resolved
@eerhardt that is better |
@jorisvergeer - it looks like there are conflicts with your change. Can you address them? The ActivatorUtilities.cs file has been moved to |
b6cde05
to
40b8991
Compare
@eerhardt Done |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Looks like there is now a test failure. Can you fix it up? |
Oh damn. I didn't think about that the tests would fail because a change in a error message. I kind of assumed that some tests were in a permanent state of failure except for private or release builds. (I've seen some of those setups). I respect this level of unit tests. |
40b8991
to
f3af090
Compare
@eerhardt Lets see how this goes. |
@eerhardt Shouldn't you have to approve this again after I change code? |
The tests are still failing. The message appears duplicated here: Line 215 in c618d42
Looks like we will need to update that message as well. |
f3af090
to
1f49efc
Compare
@eerhardt Took a while to get the tests running locally. It's passing now locally. |
The current error message gave me no pointers on what was/went wrong when I had an extra parameter to
ActivatorUtilities.CreateInstance<SomeClass>(provider, usedParameter, unusedParameter)
. I experimentally found out that this causes issues.Adding to the error message that all parameters have to be consumed would have saved me some time, and I hope that this change will save some time for a future developer.
Example that gave me unclear instructions: