Notiz
Zougrëff op dës Säit erfuerdert Autorisatioun. Dir kënnt probéieren, Iech unzemellen oder Verzeechnesser ze änneren.
Zougrëff op dës Säit erfuerdert Autorisatioun. Dir kënnt probéieren, Verzeechnesser ze änneren.
The behavior of the GetKeyedService(IServiceProvider, Type, Object) and GetKeyedServices(IServiceProvider, Type, Object) 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 usingKeyedService.AnyKeyas the lookup key.GetKeyedServices()(plural) no longer returnsAnyKeyregistrations when queried withKeyedService.AnyKey.
Version introduced
.NET 10
Previous behavior
Previously, calling GetKeyedService() with KeyedService.AnyKey returned 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.
Calling GetKeyedServices() with KeyedService.AnyKey returned all registrations for AnyKey. This behavior was also inconsistent with the intended semantics, as AnyKey is not meant to enumerate all keyed services.
New behavior
Starting in .NET 10, calling GetKeyedService() with KeyedService.AnyKey throws an InvalidOperationException. 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.
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.
var services = serviceProvider.GetKeyedServices(typeof(IMyService), KeyedService.AnyKey);
// Returns only services that were registered with a specific key.
Type of breaking change
This change is a 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
AnyKeyis treated as a special case and can't be used to resolve a single service. - Prevent
GetKeyedServices()from returningAnyKeyregistrations when queried withAnyKey.
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 and the associated merge commit.
Recommended action
If you use GetKeyedService() or GetKeyedServices() with KeyedService.AnyKey, review your code and update it to use specific keys instead of AnyKey:
- Update
GetKeyedService(KeyedService.AnyKey)calls to pass specific keys, or use alternative logic to handle service resolution. - Update
GetKeyedServices(KeyedService.AnyKey)calls to pass specific keys, or use alternative logic to enumerate only the services you intend to retrieve.
Affected APIs
- Microsoft.Extensions.DependencyInjection.ServiceProviderKeyedServiceExtensions.GetKeyedService(IServiceProvider, Type, Object)
- Microsoft.Extensions.DependencyInjection.ServiceProviderKeyedServiceExtensions.GetKeyedServices(IServiceProvider, Type, Object)