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

KeyedService.AnyKey -> not working correctly #109016

Open
waszakCeneo opened this issue Oct 18, 2024 · 5 comments
Open

KeyedService.AnyKey -> not working correctly #109016

waszakCeneo opened this issue Oct 18, 2024 · 5 comments
Labels
area-Extensions-DependencyInjection untriaged New issue has not been triaged by the area owner

Comments

@waszakCeneo
Copy link

Description

Method GetKeyedServices should allow you to get all keyed services.
Instead of adding correct override for that there was created this hack called "KeyedService.AnyKey"
The issues with this hack is that it doesn't work as intended. You would assume that its gonna return services for any key.
But no instead you have to name them the same.

Because this is confusing. I would propose adding just "provider.GetKeyedServices();" and remove AnyKey because it doesn't work as intended.

Reproduction Steps


services.AddKeyedSingleton<IMyType, MyType1>(typeof(MyType1).ToString());
services.AddKeyedSingleton<IMyType, MyType2>(typeof(MyType2).ToString());

access:
var list = provider.GetKeyedServices<IMyType>(KeyedService.AnyKey) //  empty list

Expected behavior

Returns both services

Actual behavior

Returns empty list

Regression?

No response

Known Workarounds

services.AddKeyedSingleton<IMyType, MyType1>(KeyedService.AnyKey);
services.AddKeyedSingleton<IMyType, MyType2>(KeyedService.AnyKey);

access:
var list = provider.GetKeyedServices<IMyType>(KeyedService.AnyKey) // returns both

Configuration

No response

Other information

No response

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Oct 18, 2024
Copy link
Contributor

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

@stephentoub
Copy link
Member

This prints 2 on .NET 9:

using Microsoft.Extensions.DependencyInjection;

ServiceCollection sc = new();
sc.AddKeyedSingleton<IMyType, MyType1>(typeof(MyType1).ToString());
sc.AddKeyedSingleton<IMyType, MyType2>(typeof(MyType2).ToString());
IServiceProvider provider = sc.BuildServiceProvider();

var list = provider.GetKeyedServices<IMyType>(KeyedService.AnyKey).ToList();
Console.WriteLine(list.Count);

interface IMyType { }
class MyType1 : IMyType { }
class MyType2 : IMyType { }

@waszakCeneo
Copy link
Author

waszakCeneo commented Oct 18, 2024

Doesn't work on .net8
Image

@stephentoub
Copy link
Member

Doesn't work on .net8

Correct.
#91466 (comment)
#95582
#100105

@stephentoub
Copy link
Member

cc: @benjaminpetit, @steveharter
#95853

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Extensions-DependencyInjection untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

2 participants