Describe the bug
ActivatorUtilities.CreateInstance<T>(IServiceProvider provider, params object[] parameters) does not respect ActivatorUtilitiesConstructorAttribute in a case when constructor ordering is changed.
To Reproduce
Steps to reproduce the behavior:
- Using version '3.1.6' of package 'Microsoft.Extensions.DependencyInjection'
- Run this test code:
usingMicrosoft.Extensions.DependencyInjection;usingSystem;usingXunit;namespaceUnitTests{publicclassActivatorUtilitiesConstructorTests{publicclassDependency1{}publicclassDependency2{}publicclassDependency3{}publicclassService1{publicService1(Dependency1dependency1,Dependency3dependency3){thrownewException("This should not be called.");}[ActivatorUtilitiesConstructor]publicService1(Dependency1dependency1,Dependency2dependency2,Dependency3dependency3){}}publicclassService2{[ActivatorUtilitiesConstructor]publicService2(Dependency1dependency1,Dependency2dependency2,Dependency3dependency3){}publicService2(Dependency1dependency1,Dependency3dependency3){thrownewException("This should not be called.");}}[Fact]publicvoidCreateInstance_WithActivatorUtilitiesConstructorBeingSecond_ShouldChooseMarkedCtor(){// Arrangevardependency1=newDependency1();vardependency3=newDependency3();varprovider=newServiceCollection().AddScoped<Dependency2>().BuildServiceProvider();// Act & AssertActivatorUtilities.CreateInstance<Service1>(provider,dependency1,dependency3);// This works.}[Fact]publicvoidCreateInstance_WithActivatorUtilitiesConstructorBeingFirst_ShouldChooseMarkedCtor(){// Arrangevardependency1=newDependency1();vardependency3=newDependency3();varprovider=newServiceCollection().AddScoped<Dependency2>().BuildServiceProvider();// Act & AssertActivatorUtilities.CreateInstance<Service2>(provider,dependency1,dependency3);// This fails.}}} - See error
This should not be called. when constructor marked with ActivatorUtilitiesConstructorAttribute 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.
Describe the bug
ActivatorUtilities.CreateInstance<T>(IServiceProvider provider, params object[] parameters)does not respectActivatorUtilitiesConstructorAttributein a case when constructor ordering is changed.To Reproduce
Steps to reproduce the behavior:
This should not be called.when constructor marked withActivatorUtilitiesConstructorAttributeis defined first, but no error when it is defined second.Expected behavior
I expect
ActivatorUtilitiesConstructorAttributeto 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.