-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Injected or scoped ServiceProvider does not support Keyed services #89447
Comments
Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection Issue DetailsScoped ServiceProvider, acquired from a DI scope created by Originally posted by @CarnaViire in #89446 (comment) Because of that, it is impossible to use Keyed services within HttpClientFactory now, see #89446
|
LMK if you need a runnable repro, but it should be along the lines of
You'll get an exception System.InvalidOperationException: This service provider doesn't support keyed services. |
So it's not just a scoped service provider, but also any injected service provider, so I cannot even work around this in tests by suppressing scopes. Though, to my understanding, the root cause is the same (ServiceProviderEngineScope not implementing the interface). The repro is: var services = new ServiceCollection();
services.AddKeyedSingleton<Service>("test", new Service(123));
services.AddSingleton<ServiceWithProvider>();
var sp = services.BuildServiceProvider();
var serviceWithProvider = sp.GetRequiredService<ServiceWithProvider>();
serviceWithProvider.Provider.GetRequiredKeyedService<Service>("test"); // throws InvalidOperationException
public record class Service(int Value);
public record class ServiceWithProvider(IServiceProvider Provider); |
Originally posted by @CarnaViire in #89446 (comment)
Scoped ServiceProvider, acquired from a DI scope created by
IServiceScopeFactory
, [UPD: or any injected IServiceProvider instance] does not implement theIKeyedServiceProvider
:runtime/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/ServiceProviderEngineScope.cs
Line 15 in 3dd73e6
Because of that, it is impossible to use Keyed services within HttpClientFactory now, see #89446
cc @benjaminpetit @halter73 @steveharter
The text was updated successfully, but these errors were encountered: