Description
GetConstructorAndParameters in ServiceCollectionExtensions.KeyedService.cs always selects the constructor with the most parameters:
ctor = target.GetConstructors()
.OrderByDescending(c => c.GetParameters().Length)
.First();
This does not match ActivatorUtilities.CreateInstance semantics, which considers which constructor can be best satisfied by the available services. If a type has a constructor with more parameters but not all are resolvable, this will silently select the wrong constructor and fail at runtime.
Suggestion
Consider either:
- Aligning the behavior with
ActivatorUtilities (use ActivatorUtilities.CreateFactory or similar)
- Adding prominent XML documentation warning about this divergence
- Validating that all constructor parameters can be satisfied by the
dependsOn array + registered services
The existing comment acknowledges this: // maybe we should use the same strategy of: ActivatorUtilities.CreateInstance
Description
GetConstructorAndParametersinServiceCollectionExtensions.KeyedService.csalways selects the constructor with the most parameters:This does not match
ActivatorUtilities.CreateInstancesemantics, which considers which constructor can be best satisfied by the available services. If a type has a constructor with more parameters but not all are resolvable, this will silently select the wrong constructor and fail at runtime.Suggestion
Consider either:
ActivatorUtilities(useActivatorUtilities.CreateFactoryor similar)dependsOnarray + registered servicesThe existing comment acknowledges this:
// maybe we should use the same strategy of: ActivatorUtilities.CreateInstance