Skip to content
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

Closed
CarnaViire opened this issue Jul 25, 2023 · 3 comments · Fixed by #89509
Closed

Injected or scoped ServiceProvider does not support Keyed services #89447

CarnaViire opened this issue Jul 25, 2023 · 3 comments · Fixed by #89509

Comments

@CarnaViire
Copy link
Member

CarnaViire commented Jul 25, 2023

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 the IKeyedServiceProvider:

internal sealed class ServiceProviderEngineScope : IServiceScope, IServiceProvider, IAsyncDisposable, IServiceScopeFactory

Because of that, it is impossible to use Keyed services within HttpClientFactory now, see #89446

cc @benjaminpetit @halter73 @steveharter

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jul 25, 2023
@ghost
Copy link

ghost commented Jul 25, 2023

Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection
See info in area-owners.md if you want to be subscribed.

Issue Details

Scoped ServiceProvider, acquired from a DI scope created by IServiceScopeFactory, does not implement the IKeyedServiceProvider (see ServiceProviderEngineScope.cs)

Originally posted by @CarnaViire in #89446 (comment)

Because of that, it is impossible to use Keyed services within HttpClientFactory now, see #89446

Author: CarnaViire
Assignees: -
Labels:

area-Extensions-DependencyInjection

Milestone: -

@CarnaViire
Copy link
Member Author

LMK if you need a runnable repro, but it should be along the lines of

  1. Register a keyed singleton
  2. Build ServiceProvider, resolve IServiceScopeFactory and create a scope
  3. Try to resolve the keyed singleton from scope's service provider.

You'll get an exception System.InvalidOperationException: This service provider doesn't support keyed services.

@CarnaViire
Copy link
Member Author

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);

@CarnaViire CarnaViire changed the title Scoped ServiceProvider does not support Keyed services Injected or scoped ServiceProvider does not support Keyed services Jul 25, 2023
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jul 26, 2023
@ghost ghost removed in-pr There is an active PR which will close this issue when it is merged untriaged New issue has not been triaged by the area owner labels Jul 28, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Aug 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant