-
Notifications
You must be signed in to change notification settings - Fork 5.3k
DI container should respect generic constraints when resolving single instance #123255
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
Conversation
|
@dotnet-policy-service agree |
|
Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection |
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.
Pull request overview
This PR fixes a bug in the DI container where resolving a single service instance (GetService) could incorrectly return an earlier compatible open generic registration when the most recent registration had incompatible generic constraints, violating the "last wins" principle.
Changes:
- Updated
CallSiteFactoryto track the slot counter even when the last open generic registration has incompatible constraints, ensuring single service resolution correctly attempts to use the last registration and throwsArgumentException - Added a test case to verify that incompatible constrained open generics are skipped in enumerable resolution but cause exceptions in single service resolution
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/CallSiteFactory.cs | Updated slot tracking logic to include the case where the last registration has incompatible constraints, ensuring proper "last wins" semantics |
| src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.Tests/ServiceProviderContainerTests.cs | Added test case to verify the fix works correctly for both enumerable and single service resolution scenarios |
src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/CallSiteFactory.cs
Outdated
Show resolved
Hide resolved
...ies/Microsoft.Extensions.DependencyInjection/tests/DI.Tests/ServiceProviderContainerTests.cs
Outdated
Show resolved
Hide resolved
...ies/Microsoft.Extensions.DependencyInjection/tests/DI.Tests/ServiceProviderContainerTests.cs
Outdated
Show resolved
Hide resolved
CarnaViire
left a comment
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
fixes #108874
DI filters out open generics with incompatible constraints by default. However, if the most recently registered open generic has incompatible constraints, DI skips it during IEnumerable resolution and falls back to the previous compatible registration. If that earlier registration is compatible, DI caches it as the “last” valid one. As a result, when users resolve a service whose most recent implementation had incompatible constraints, but an earlier one was compatible, they receive an implementation that is not the last registered. This breaks the usual “last wins” rule of DI containers and can lead to confusing, nearly impossible to debug behaviour.