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

not support keyed services when use scoped service #90235

Closed
WeihanLi opened this issue Aug 9, 2023 · 2 comments
Closed

not support keyed services when use scoped service #90235

WeihanLi opened this issue Aug 9, 2023 · 2 comments

Comments

@WeihanLi
Copy link
Contributor

WeihanLi commented Aug 9, 2023

Description

When trying to use the keyed service with scoped lifetime, InvalidOperationException thrown with This service provider doesn't support keyed services message

Reproduction Steps

var serviceCollection = new ServiceCollection();
serviceCollection.AddKeyedScoped<IIdGenerator, GuidIdGenerator>("guid");
using var services = serviceCollection.BuildServiceProvider();
using var scope = services.CreateScope();
var newId = scope.ServiceProvider.GetRequiredKeyedService<IIdGenerator>("guid")
    .NewId();
Console.WriteLine(newId);


file interface IIdGenerator
{
    string NewId();
}

file sealed class GuidIdGenerator : IIdGenerator
{
    public string NewId() => Guid.NewGuid().ToString("N");
}

Expected behavior

Output a Guid value without exception

Actual behavior

Exception with the stack trace below

System.InvalidOperationException: This service provider doesn't support keyed services.
   at Microsoft.Extensions.DependencyInjection.ServiceProviderKeyedServiceExtensions.GetRequiredKeyedService(IServiceProvider provider, Type serviceType, Object serviceKey)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderKeyedServiceExtensions.GetRequiredKeyedService[T](IServiceProvider provider, Object serviceKey)

Regression?

No response

Known Workarounds

No response

Configuration

.NET 8 Preview 7, 8.0.100-preview.7.23376.3
Windows 11 x64

Other information

No response

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

ghost commented Aug 9, 2023

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

Issue Details

Description

When trying to use the keyed service with scoped lifetime, InvalidOperationException thrown with This service provider doesn't support keyed services message

Reproduction Steps

var serviceCollection = new ServiceCollection();
serviceCollection.AddKeyedScoped<IIdGenerator, GuidIdGenerator>("guid");
using var services = serviceCollection.BuildServiceProvider();
using var scope = services.CreateScope();
var newId = scope.ServiceProvider.GetRequiredKeyedService<IIdGenerator>("guid")
    .NewId();
Console.WriteLine(newId);


file interface IIdGenerator
{
    string NewId();
}

file sealed class GuidIdGenerator : IIdGenerator
{
    public string NewId() => Guid.NewGuid().ToString("N");
}

Expected behavior

Output a Guid value without exception

Actual behavior

Exception with the stack trace below

System.InvalidOperationException: This service provider doesn't support keyed services.
   at Microsoft.Extensions.DependencyInjection.ServiceProviderKeyedServiceExtensions.GetRequiredKeyedService(IServiceProvider provider, Type serviceType, Object serviceKey)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderKeyedServiceExtensions.GetRequiredKeyedService[T](IServiceProvider provider, Object serviceKey)

Regression?

No response

Known Workarounds

No response

Configuration

.NET 8 Preview 7, 8.0.100-preview.7.23376.3
Windows 11 x64

Other information

No response

Author: WeihanLi
Assignees: -
Labels:

untriaged, area-Extensions-DependencyInjection

Milestone: -

@WeihanLi
Copy link
Contributor Author

WeihanLi commented Aug 9, 2023

Seemed already fixed by #89509 closed

@WeihanLi WeihanLi closed this as completed Aug 9, 2023
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Aug 9, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Sep 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant