Skip to content

Dependency Injection: Keyed services returned for non-keyed resolution #113022

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

Closed
kmcclellan opened this issue Feb 28, 2025 · 2 comments
Closed

Comments

@kmcclellan
Copy link

Description

The relatively new feature of keyed services seems to have a bug. I would not expect a keyed service to be returned from the non-keyed call to IServiceProvider.GetServices<T>() extension method.

Reproduction Steps

using Microsoft.Extensions.DependencyInjection;

var services = new ServiceCollection();
services.AddKeyedTransient<TestProcessor>("test123");

await using var provider = services.BuildServiceProvider();

for (var i = 0; i < 10; i++)
{
    var processors = new List<TestProcessor>();

    // Common pattern for combining both specific and global logic.
    processors.AddRange(provider.GetKeyedServices<TestProcessor>("test123"));
    processors.AddRange(provider.GetServices<TestProcessor>());

    // Only a single processor is registered, so this should always be 1?
    Console.WriteLine("Processor count: {0}", processors.Count);

    // Not sure why the delay makes a difference, but it does.
    await Task.Delay(1);
}

sealed class TestProcessor
{
}

Expected behavior

The number of processors resolved should be consistent with each invocation. The number of processors resolved should equal one.

Actual behavior

Processor count: 1
Processor count: 1
Processor count: 1
Processor count: 1
Processor count: 2
Processor count: 2
Processor count: 2
Processor count: 2
Processor count: 2
Processor count: 2

Regression?

Does not appear to be a regression (reproducible with Microsoft.Extensions.DependencyInjection v8.0.0).

Known Workarounds

No response

Configuration

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.2" />
  </ItemGroup>

</Project>

.NET SDK 9.0.102
Windows 11 10.0.26100

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 Feb 28, 2025
Copy link
Contributor

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

@KalleOlaviNiemitalo
Copy link

KalleOlaviNiemitalo commented Mar 1, 2025

This seems a duplicate of #111795. Workaround:

AppContext.SetSwitch("Microsoft.Extensions.DependencyInjection.DisableDynamicEngine", true);

@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Mar 7, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Apr 7, 2025
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

3 participants