Skip to content

Keyed services are not being returend by GetServices<T>  #95576

Description

@tomfrenzel

Description

The release of .NET 8 allowed to register services using an optional key. When doing so, the only way to retrieve the service is by using the GetKeyedService methods.
I would expect the GetServices<T> method to also return this keyed service

Reproduction Steps

The following test fails:

var provider = new ServiceCollection()
    .Add<Car, FastCar>("KeyedCar")
    .BuildServiceProvider();

var registeredCars = provider.GetServices<Car>().ToList();

Assert.AreEqual(1, registeredCars.Count);

Expected behavior

The GetServices<T>(this IServiceProvider provider) method should return all registered implementations of T not matter if they have been registered with a key or not

Actual behavior

The GetServices<T>(this IServiceProvider provider) method only returns services that have been registered without a key

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions