Skip to content

Correct keyed-service probing for built-in DI services#128198

Merged
rosebyte merged 4 commits into
mainfrom
copilot/fix-keyed-services-claim
May 20, 2026
Merged

Correct keyed-service probing for built-in DI services#128198
rosebyte merged 4 commits into
mainfrom
copilot/fix-keyed-services-claim

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 14, 2026

IServiceProviderIsKeyedService was reporting built-in DI service types as available for arbitrary keys, while actual keyed resolution correctly failed. This change aligns probing with resolvability by treating built-ins as available only for the null key.

  • Behavior fix (CallSiteFactory.IsService)

    • Restrict built-in service fallback (IServiceProvider, IServiceScopeFactory, IServiceProviderIsService, IServiceProviderIsKeyedService) to serviceKey is null.
    • Preserve existing descriptor/AnyKey lookup behavior for explicitly registered keyed services.
  • Regression coverage (spec tests)

    • Add theory coverage for all four built-in service types.
    • Assert:
      • IsKeyedService(type, null) == true
      • IsKeyedService(type, nonNullKey) == false
      • IsKeyedService(type, KeyedService.AnyKey) == false
// New expected behavior for built-ins:
Assert.True(spiks.IsKeyedService(typeof(IServiceProvider), null));
Assert.False(spiks.IsKeyedService(typeof(IServiceProvider), "foo"));
Assert.False(spiks.IsKeyedService(typeof(IServiceProvider), KeyedService.AnyKey));

Fixes #127537

Copilot AI review requested due to automatic review settings May 14, 2026 14:37
Copilot AI review requested due to automatic review settings May 14, 2026 14:37
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

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

Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/f4149def-49de-43e5-84a5-a346b3d05cc4

Co-authored-by: rosebyte <14963300+rosebyte@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot May 14, 2026 15:05
Copilot AI requested review from Copilot and removed request for Copilot May 14, 2026 15:23
Copilot AI changed the title [WIP] Fix misleading claims about built-in services as keyed services Correct keyed-service probing for built-in DI services May 14, 2026
Copilot AI requested a review from rosebyte May 14, 2026 15:25
@rosebyte rosebyte marked this pull request as ready for review May 14, 2026 19:49
Copilot AI review requested due to automatic review settings May 14, 2026 19:49
@rosebyte rosebyte requested review from mrek-msft and svick May 14, 2026 19:50
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes keyed-service probing in the default Microsoft.Extensions.DependencyInjection provider so built-in DI services are reported as keyed-resolvable only when the service key is null, matching actual resolution behavior.

Changes:

  • Gates built-in DI service fallback in CallSiteFactory.IsService on ServiceKey is null.
  • Adds specification regression coverage for the four built-in DI service types with null, non-null, and KeyedService.AnyKey probes.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/CallSiteFactory.cs Aligns built-in service probing with keyed resolution semantics.
src/libraries/Microsoft.Extensions.DependencyInjection.Specification.Tests/src/ServiceProviderIsKeyedServiceSpecificationTests.cs Adds regression coverage for keyed probing of built-in DI services.

@rosebyte rosebyte enabled auto-merge (squash) May 19, 2026 11:38
@rosebyte rosebyte merged commit c940d91 into main May 20, 2026
90 of 93 checks passed
@rosebyte rosebyte deleted the copilot/fix-keyed-services-claim branch May 20, 2026 12:09
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview6 milestone May 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Default Service Provider falsely claims built-in services are available as keyed services

5 participants