-
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
Merged
Merged
Document breaking change for GetKeyedService and GetKeyedServices with AnyKey in .NET 10 Preview 3 #50053
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
docs/core/compatibility/extensions/10.0/getkeyedservice-anykey.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| --- | ||
| title: "Breaking change: Fix issues in GetKeyedService() and GetKeyedServices() with AnyKey" | ||
| description: "Learn about the breaking change in .NET 10 Preview 3 where GetKeyedService() and GetKeyedServices() behavior changed when using KeyedService.AnyKey as the lookup key." | ||
| ms.date: 11/19/2025 | ||
| ai-usage: ai-assisted | ||
| --- | ||
|
|
||
| # Fix issues in GetKeyedService() and GetKeyedServices() with AnyKey | ||
|
|
||
| The behavior of `GetKeyedService()` and `GetKeyedServices()` methods in the `Microsoft.Extensions.DependencyInjection` library was updated to address inconsistencies in handling the `KeyedService.AnyKey` registration. Specifically, `GetKeyedService()` now throws an exception when you attempt to resolve a single service using `KeyedService.AnyKey` as the lookup key, and `GetKeyedServices()` no longer returns `AnyKey` registrations when queried with `KeyedService.AnyKey`. | ||
gewarren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Version introduced | ||
|
|
||
| .NET 10 Preview 3 | ||
gewarren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Previous behavior | ||
|
|
||
| Previously, calling `GetKeyedService()` with `KeyedService.AnyKey` would return a service registration associated with `AnyKey`. This behavior was inconsistent with the intended semantics, as `AnyKey` is meant to represent a special case of keyed services rather than a specific registration. | ||
|
|
||
| Additionally, calling `GetKeyedServices()` with `KeyedService.AnyKey` would return all registrations for `AnyKey`. This behavior was also inconsistent with the intended semantics, as `AnyKey` is not meant to enumerate all keyed services. | ||
gewarren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## New behavior | ||
|
|
||
| Starting in .NET 10, calling `GetKeyedService()` with `KeyedService.AnyKey` throws an exception. This ensures that `AnyKey` can't be used to resolve a single service, as it's intended to represent a special case rather than a specific key. | ||
gewarren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```csharp | ||
| var service = serviceProvider.GetKeyedService(typeof(IMyService), KeyedService.AnyKey); | ||
| // Throws InvalidOperationException: "Cannot resolve a single service using AnyKey." | ||
| ``` | ||
|
|
||
| Additionally, calling `GetKeyedServices()` with `KeyedService.AnyKey` no longer returns registrations for `AnyKey`. Instead, it adheres to the updated semantics where `AnyKey` is treated as a special case and doesn't enumerate services. | ||
|
|
||
| ```csharp | ||
| var services = serviceProvider.GetKeyedServices(typeof(IMyService), KeyedService.AnyKey); | ||
| // Returns an empty collection. | ||
| ``` | ||
|
|
||
| ## Type of breaking change | ||
|
|
||
| This change is a [behavioral change](../../categories.md#behavioral-change). | ||
|
|
||
| ## Reason for change | ||
|
|
||
| The previous behavior of `GetKeyedService()` and `GetKeyedServices()` with `KeyedService.AnyKey` was inconsistent with the intended semantics of `AnyKey`. The changes were introduced to ensure that `AnyKey` is treated as a special case and can't be used to resolve a single service, and to prevent `GetKeyedServices()` from returning `AnyKey` registrations when queried with `AnyKey`. These updates improve the predictability and correctness of the `Microsoft.Extensions.DependencyInjection` library's behavior when working with keyed services. For more details, see the [pull request](https://github.com/dotnet/runtime/pull/113137) and the associated [merge commit](https://github.com/dotnet/runtime/commit/deee462fc8421a7e18b8916eb5a5eacb9d09169d). | ||
|
|
||
| ## Recommended action | ||
|
|
||
| Developers using `GetKeyedService()` or `GetKeyedServices()` with `KeyedService.AnyKey` should review their code and update it to use specific keys instead of `AnyKey`. | ||
gewarren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| For `GetKeyedService(KeyedService.AnyKey)`, replace calls to `GetKeyedService()` with `KeyedService.AnyKey` with specific keys or alternative logic to handle service resolution. | ||
|
|
||
gewarren marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| **Before**: | ||
|
|
||
| ```csharp | ||
| var service = serviceProvider.GetKeyedService(typeof(IMyService), KeyedService.AnyKey); | ||
| ``` | ||
|
|
||
| **After**: | ||
|
|
||
| ```csharp | ||
| // Replace AnyKey with a specific key or implement custom logic for service resolution. | ||
| var service = serviceProvider.GetKeyedService(typeof(IMyService), specificKey); | ||
| ``` | ||
|
|
||
| For `GetKeyedServices(KeyedService.AnyKey)`, update code to explicitly enumerate or query services by specific keys instead of relying on `AnyKey`. | ||
|
|
||
| **Before**: | ||
|
|
||
| ```csharp | ||
| var services = serviceProvider.GetKeyedServices(typeof(IMyService), KeyedService.AnyKey); | ||
| ``` | ||
|
|
||
| **After**: | ||
|
|
||
| ```csharp | ||
| // Replace AnyKey with specific keys or implement custom logic for service enumeration. | ||
| var services = serviceProvider.GetKeyedServices(typeof(IMyService), specificKey); | ||
| ``` | ||
|
|
||
gewarren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ## Affected APIs | ||
|
|
||
| - <xref:Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetKeyedService(System.IServiceProvider,System.Type,System.Object)?displayProperty=fullName> | ||
| - <xref:Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetKeyedServices(System.IServiceProvider,System.Type,System.Object)?displayProperty=fullName> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.