-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
ActivatorUtilitiesConstructorAttribute Does Not Work in Some Cases #42339
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging subscribers to this area: @eerhardt, @maryamariyan |
Closing as dupe of #46132 since there is more discussion there |
Using [ActivatorUtilitiesconstructor] attribute on a constructor should force `ActivatorUtilities.CreateInstance` to use that constructor regardless of the constructor definition order. This test shows that it is not the case in current implementaion.
Previously `ActivatorUtilities.CreateInstance` and `ActivatorUtilities.CreateFactory` behaved differently: former depended on ctor definition order to disambiguate ctors, while latter failed altogether. The behavior is now unified and addresses concerns raised in dotnet#45119 dotnet#42339 and dotnet#46132. Constructors are chosen based on the following factors in a given order: 1) Preferred ctor (having [ActivatorUtilitiesConstructor] attribute) 2) Ctor with the most surjective parameters based on the given arguments. In other words constructor that has the least unresolved parameters is chosen. 3) Ctor with more resolved parameters is prefered. If two ctors have all of the supplied arguments, but one of them has additional parameters with default values, then the one with more parameters is chosen.
Note that this issue was closed, but there are still issues with the attribute not working. For example adding this to the original repro fails: public class Service3
{
[ActivatorUtilitiesConstructor]
public Service3(Dependency1 dependency1, Dependency3 dependency3)
{
}
public Service3(Dependency1 dependency1, Dependency2 dependency2, object? o)
{
throw new Exception("This should not be called.");
}
}
[Fact]
public void Test()
{
var dependency1 = new Dependency1();
var dependency3 = new Dependency3();
var provider = new ServiceCollection()
.AddScoped<Dependency2>()
.BuildServiceProvider();
// This should call (d1, d3) but instead calls (d1, d2, null).
ActivatorUtilities.CreateInstance<Service3>(provider, dependency1, dependency3);
} |
Describe the bug
ActivatorUtilities.CreateInstance<T>(IServiceProvider provider, params object[] parameters)
does not respectActivatorUtilitiesConstructorAttribute
in a case when constructor ordering is changed.To Reproduce
Steps to reproduce the behavior:
This should not be called.
when constructor marked withActivatorUtilitiesConstructorAttribute
is defined first, but no error when it is defined second.Expected behavior
I expect
ActivatorUtilitiesConstructorAttribute
to be respected regardless of the order in which constructors were defined.Additional context
The given example is reproduced using 3 dependencies. Removing one dependency makes both tests pass. Maybe this will help to get on a right path.
The text was updated successfully, but these errors were encountered: