-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Document breaking change for GetKeyedService and GetKeyedServices with AnyKey in .NET 10 Preview 3 #50053
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
…vices with AnyKey Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com>
docs/core/compatibility/extensions/10.0/getkeyedservice-anykey.md
Outdated
Show resolved
Hide resolved
docs/core/compatibility/extensions/10.0/getkeyedservice-anykey.md
Outdated
Show resolved
Hide resolved
docs/core/compatibility/extensions/10.0/getkeyedservice-anykey.md
Outdated
Show resolved
Hide resolved
docs/core/compatibility/extensions/10.0/getkeyedservice-anykey.md
Outdated
Show resolved
Hide resolved
docs/core/compatibility/extensions/10.0/getkeyedservice-anykey.md
Outdated
Show resolved
Hide resolved
docs/core/compatibility/extensions/10.0/getkeyedservice-anykey.md
Outdated
Show resolved
Hide resolved
docs/core/compatibility/extensions/10.0/getkeyedservice-anykey.md
Outdated
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.
Pull Request Overview
This PR documents breaking behavioral changes in GetKeyedService() and GetKeyedServices() methods when using KeyedService.AnyKey in .NET 10 Preview 3. The changes fix inconsistencies in how these methods handled the special AnyKey registration.
Key changes:
- New breaking change article documenting the exception thrown by
GetKeyedService()and the empty collection returned byGetKeyedServices()when usingAnyKey. - Updated table of contents and .NET 10 index to include the new breaking change entry.
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| docs/core/compatibility/toc.yml | Added navigation entry for the new breaking change article |
| docs/core/compatibility/extensions/10.0/getkeyedservice-anykey.md | New breaking change documentation with behavior details, code examples, and migration guidance |
| docs/core/compatibility/10.0.md | Added table entry for the breaking change in the Extensions section |
BillWagner
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.
This LGTM @gewarren
Let's ![]()
Summary
Documents breaking behavioral changes in
GetKeyedService()andGetKeyedServices()when usingKeyedService.AnyKeyin .NET 10 Preview 3.Changes
Added breaking change article:
docs/core/compatibility/extensions/10.0/getkeyedservice-anykey.mdGetKeyedService()now throwsInvalidOperationExceptionwhen called withKeyedService.AnyKeyGetKeyedServices()returns empty collection instead ofAnyKeyregistrationsUpdated navigation: Added entries to
toc.ymland10.0.mdindex under Extensions sectionExample
Related: dotnet/runtime#113137
Original prompt
This section details on the original issue you should resolve
<issue_title>[Breaking change]: Fix issues in GetKeyedService() and GetKeyedServices() with AnyKey</issue_title>
<issue_description>### Description
The behavior of
GetKeyedService()andGetKeyedServices()methods in theMicrosoft.Extensions.DependencyInjectionlibrary has been updated to address inconsistencies in handling theAnyKeyregistration. Specifically:GetKeyedService()method now throws an exception when attempting to resolve a single service usingKeyedService.AnyKeyas the lookup key.GetKeyedServices()method no longer returnsAnyKeyregistrations when queried withKeyedService.AnyKey.These changes were introduced to improve consistency in the behavior of keyed service resolution and align with the intended semantics of
KeyedService.AnyKey.Version
.NET 10 Preview 3
Previous behavior
GetKeyedService(KeyedService.AnyKey):GetKeyedService()withKeyedService.AnyKeywould return a service registration associated withAnyKey. This behavior was inconsistent with the intended semantics, asAnyKeyis meant to represent a special case of keyed services rather than a specific registration.GetKeyedServices(KeyedService.AnyKey):GetKeyedServices()withKeyedService.AnyKeywould return all registrations forAnyKey. This behavior was inconsistent with the intended semantics, asAnyKeyis not meant to enumerate all keyed services.New behavior
GetKeyedService(KeyedService.AnyKey):GetKeyedService()withKeyedService.AnyKeynow throws an exception. This ensures thatAnyKeycannot be used to resolve a single service, as it is intended to represent a special case rather than a specific key.Example:
GetKeyedServices(KeyedService.AnyKey):GetKeyedServices()withKeyedService.AnyKeyno longer returns registrations forAnyKey. Instead, it adheres to the updated semantics whereAnyKeyis treated as a special case and does not enumerate services.Example:
Type of breaking change
Reason for change
The previous behavior of
GetKeyedService()andGetKeyedServices()withKeyedService.AnyKeywas inconsistent with the intended semantics ofAnyKey. The changes were introduced to:AnyKeyis treated as a special case and cannot be used to resolve a single service.GetKeyedServices()from returningAnyKeyregistrations when queried withAnyKey.These updates improve the predictability and correctness of the
Microsoft.Extensions.DependencyInjectionlibrary's behavior when working with keyed services.Recommended action
Developers using
GetKeyedService()orGetKeyedServices()withKeyedService.AnyKeyshould review their code and update it as follows:For
GetKeyedService(KeyedService.AnyKey):GetKeyedService()withKeyedService.AnyKeywith specific keys or alternative logic to handle service resolution.Before:
After:
For
GetKeyedServices(KeyedService.AnyKey):AnyKey.Before:
After:
Feature area
Affected APIs
Microsoft.Extensions.DependencyInjection.ServiceProvider.GetKeyedService(Type, object)💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.
Internal previews