Repository navigation
Description
Description
I encountered an issue with IServiceProvider.GetKeyedServices() in .NET 8 and .NET 9 where it creates new instances of Singleton services instead of returning existing ones when you mix using of KeyedService.AnyKey and of specific keys.
Reproduction Steps
- Create a new .NET project.
- Add the following dependencies:
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.3" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.3" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" /> <PackageReference Include="MSTest" Version="3.8.2" />
- Add the following test code:
[TestClass] public sealed class Test1 { [TestMethod] public void TestMethod1() { var services = new ServiceCollection(); services.AddKeyedSingleton<TestService>("key1"); services.AddKeyedSingleton<TestService>("key2"); services.AddKeyedSingleton<TestService>("key3"); var serviceProvider = services.BuildServiceProvider(); var allInstances = serviceProvider.GetKeyedServices<TestService>(KeyedService.AnyKey).ToArray(); var service1 = serviceProvider.GetKeyedService<TestService>("key1"); var service2 = serviceProvider.GetKeyedService<TestService>("key2"); var service3 = serviceProvider.GetKeyedService<TestService>("key3"); Assert.AreEqual(3, allInstances.Length); Assert.Contains(service1, allInstances); Assert.Contains(service2, allInstances); Assert.Contains(service3, allInstances); } } public class TestService { public string Id { get; set; } = Guid.NewGuid().ToString(); public override bool Equals(object obj) { if (obj is TestService other) { return Id == other.Id; } return false; } public override int GetHashCode() { return Id.GetHashCode(); } }
Expected behavior
The test should pass, and GetKeyedService<TestService>("key1") should return the same instance that was created when calling GetKeyedServices<TestService>(KeyedService.AnyKey).
Actual behavior
The test fails because GetKeyedService<TestService>("key1") creates a new instance instead of returning the existing one. This issue persists regardless of the order in which the services are requested: when calling GetKeyedServices<TestService>("key1") first, it also creates new instances when calling GetKeyedServices<TestService>(KeyedService.AnyKey) instead of returning the already created ones.
Regression?
No response
Known Workarounds
It works if you register your singletons in the following way:
services.AddKeyedSingleton("key1", (sp, key) => key== KeyedService.AnyKey ? sp.GetKeyedService<TestService>("key1") : new TestService() );
services.AddKeyedSingleton("key2", (sp, key) => key == KeyedService.AnyKey ? sp.GetKeyedService<TestService>("key2") : new TestService());
services.AddKeyedSingleton("key3", (sp, key) => key == KeyedService.AnyKey ? sp.GetKeyedService<TestService>("key3") : new TestService());However, I'm not sure if this is correct workaround and if won't cause any side effects.
Configuration
.NET 8 and .NET 9
Microsoft.Extensions.Hosting Version: 9.0.3
Microsoft.Extensions.DependencyInjection Version: 9.0.3
Other information
A similar issue was previously fixed for services registered with specific instances using IServiceCollection.AddKeyedSingleton(object? serviceKey, TService implementationInstance) in #95582. However, the problem with services created by the DI container itself remains
Activity
dotnet-policy-service commentedon Mar 13, 2025
Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection
See info in area-owners.md if you want to be subscribed.
KalleOlaviNiemitalo commentedon Mar 14, 2025
Does #113137 fix this?
denisbredikhin commentedon Mar 14, 2025
I don't see my scenario in the unit tests added in this PR.
In my case I don't register specific instance, but register using just the type.
steveharter commentedon Mar 14, 2025
@benjaminpetit can you take a look - with AnyKey, should
GetKeyedServices(KeyedService.AnyKey)contain the same instances as would be returned from
GetKeyedService(somekey)IliaShuliatikov commentedon Mar 14, 2025
This happens because
GetKeyedServices(KeyedService.AnyKey)callsTryCreateExactfrom theTryCreateEnumerablemethod 3 times, passing 0, 1, 2 as theslotargument, which is part of the cache entry.GetKeyedService(somekey), on the other hand, callsTryCreateExactfrom theTryCreateEnumerablemethod only 1 time, passing 0 as theslotargument. Hence, there is 1 cache key match when the cached entry hasslotequal to 0 and 2 mismatches for cache entries withslotequal to 1 and 2.steveharter commentedon Mar 24, 2025
Moving to v10 for additional verification.
steveharter commentedon Mar 24, 2025
We should also validate this issue against the latest v10 code base since there have been some recent cache issues fixed.
steveharter commentedon Apr 23, 2025
After investigating, this issue still exists and is because each keyed service (service Type + service key) needs to maintain its own "slot" count. I will put up a PR soon.
IliaShuliatikov commentedon Apr 23, 2025
@steveharter, are you considering external contribution for this issue? I have recently worked on another DI issue with the same methods and have investigated this issue, hence understand why this is happening, so I'm pretty confident I can fix this quickly with an optimal solution and would be happy to help
steveharter commentedon Apr 23, 2025
I already have a fix. I will ping you on the review. Thanks.