Skip to content

ServiceProvider.GetKeyedServices<T>() creates new instances of Singleton services instead of returning existing ones when you request them with KeyedService.AnyKey #113472

Closed

Description

@denisbredikhin

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

  1. Create a new .NET project.
  2. 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" />
  3. 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

  1. added
    untriaged
    on Mar 13, 2025
  2. dotnet-policy-service commented on Mar 13, 2025

    @dotnet-policy-service
    Contributor

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

  3. KalleOlaviNiemitalo commented on Mar 14, 2025

    @KalleOlaviNiemitalo

    Does #113137 fix this?

  4. denisbredikhin commented on Mar 14, 2025

    @denisbredikhin
    Author

    Does #113137 fix this?

    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.

  5. steveharter commented on Mar 14, 2025

    @steveharter
    Contributor

    @benjaminpetit can you take a look - with AnyKey, should
    GetKeyedServices(KeyedService.AnyKey)
    contain the same instances as would be returned from
    GetKeyedService(somekey)

  6. IliaShuliatikov commented on Mar 14, 2025

    @IliaShuliatikov
    Contributor

    This happens because GetKeyedServices(KeyedService.AnyKey) calls TryCreateExact from the TryCreateEnumerable method 3 times, passing 0, 1, 2 as the slot argument, which is part of the cache entry.
    GetKeyedService(somekey), on the other hand, calls TryCreateExact from the TryCreateEnumerable method only 1 time, passing 0 as the slot argument. Hence, there is 1 cache key match when the cached entry has slot equal to 0 and 2 mismatches for cache entries with slot equal to 1 and 2.

  7. steveharter commented on Mar 24, 2025

    @steveharter
    Contributor

    Moving to v10 for additional verification.

  8. added this to the 10.0.0 milestone on Mar 24, 2025
  9. removed
    untriaged
    on Mar 24, 2025
  10. added
    help wanted
    untriaged
    and removed
    untriaged
    on Mar 24, 2025
  11. steveharter commented on Mar 24, 2025

    @steveharter
    Contributor

    We should also validate this issue against the latest v10 code base since there have been some recent cache issues fixed.

  12. removed
    help wanted
    on Mar 24, 2025
  13. steveharter commented on Apr 23, 2025

    @steveharter
    Contributor

    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.

  14. IliaShuliatikov commented on Apr 23, 2025

    @IliaShuliatikov
    Contributor

    @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

  15. steveharter commented on Apr 23, 2025

    @steveharter
    Contributor

    I already have a fix. I will ping you on the review. Thanks.

  16. locked and limited conversation to collaborators on May 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions