-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Fix issues in GetKeyedService() and GetKeyedServices() with AnyKey #113137
Conversation
…ey as the lookup key
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.
PR Overview
This PR addresses inconsistencies in the resolution and caching behaviors for KeyedService.AnyKey. It updates the logic in service call site creation, modifies the service provider methods to validate AnyKey usage, and revises tests to ensure the correct behavior and exception throwing.
Reviewed Changes
File | Description |
---|---|
ServiceLookup/CallSiteFactory.cs | Adjusted the keys matching logic and caching behavior to exclude AnyKey registrations while preserving proper lookups. |
Specification.Tests/KeyedDependencyInjectionSpecificationTests.cs | Updated tests to reflect the revised expectations for service counts and exception throwing regarding AnyKey usage. |
ServiceProvider.cs | Added explicit checks to throw exceptions when AnyKey is used to resolve a non-collection service. |
ServiceLookup/ThrowHelper.cs | Introduced a helper method to throw an InvalidOperationException for invalid AnyKey resolution attempts. |
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
...ns.DependencyInjection.Specification.Tests/src/KeyedDependencyInjectionSpecificationTests.cs
Show resolved
Hide resolved
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. I think having GetKeyedServices<T>(KeyedService.AnyKey)
return all registrations for each specific key rather than the last one is fine. I was just curious if @stephentoub had any opinions since he first asked for AnyKey
to even work with GetKeyedServices
.
It would still be nice if we could assert the right order is maintained for the services returned by GetKeyedServices
in the QueryWithIEnumerable
test. I don't think the order is wrong, but it'd be nice to veriffy.
Reviving the closed v9.0 PR #97561:
After this PR is in, I'll update push another PR to update the docs around IEnumerable semantics.
Semantics here mostly related to fallout and discussions from #95582. Here's a summary of semantics copied from a test added in this PR: