Skip to content

Align keyed service tests with MS.DI spec and fix AnyKey enumeration#613

Merged
seesharper merged 3 commits into
masterfrom
fix-keyed-tests
May 21, 2026
Merged

Align keyed service tests with MS.DI spec and fix AnyKey enumeration#613
seesharper merged 3 commits into
masterfrom
fix-keyed-tests

Conversation

@seesharper
Copy link
Copy Markdown
Owner

@seesharper seesharper commented May 21, 2026

Summary

  • Aligns KeyedMicrosoftTests.cs with the upstream KeyedDependencyInjectionSpecificationTests.cs from dotnet/runtime
  • Fixes a bug where AnyKey ("*") registrations were incorrectly included in IEnumerable<T> keyed service resolution
  • Bumps version to 7.1.0

Changes

Tests (KeyedMicrosoftTests.cs)

  • Updated ResolveKeyedServicesAnyKeyWithAnyKeyRegistration: expects 4 services (not 5); AnyKey registrations are excluded from enumeration
  • Updated ResolveKeyedServicesSingletonInstanceWithAnyKey: AnyKey registrations are not included when listing services by a specific key
  • Updated ResolveKeyedSingletonFromScopeServiceProvider and ResolveKeyedScopedFromScopeServiceProvider: added assertions that direct AnyKey lookup throws
  • Added 13 new tests: CombinationalRegistration, ResolveKeyedServicesAnyKeyConsistency, ResolveKeyedServicesAnyKeyConsistencyWithAnyKeyRegistration, ResolveKeyedServicesAnyKeyOrdering, ResolveWithAnyKeyQuery_Constructor, ResolveWithAnyKeyQuery_Constructor_Duplicates, ResolveWithAnyKeyQuery_InstanceProvided, ResolveWithAnyKeyQuery_InstanceProvided_Duplicates, ResolveRequiredKeyedServiceThrowsIfNotFound, SimpleServiceKeyedResolution

Fix (LightInject.cs)

In CreateEmitMethodForEnumerableServiceServiceRequest, two cases fixed (both variance and no-variance branches):

  • GetKeyedServices(AnyKey): added && k.ServiceName != "*" so the "*" registration itself is not returned
  • GetKeyedServices("some-key"): removed || k.ServiceName == "*" so AnyKey wildcard registrations don't appear in specific-key enumerations

Single-service resolution with AnyKey wildcard fallback is unaffected.

Test plan

  • All 1561 tests pass

🤖 Generated with Claude Code

seesharper and others added 3 commits May 21, 2026 13:43
…ationTests

Update existing tests to match new MS.DI expected behavior (AnyKey registrations
excluded from enumerations) and add 13 new tests covering combinational registration,
AnyKey consistency/ordering, constructor/instance-provided AnyKey queries, and
simple keyed resolution via IServiceFactory.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
When building IEnumerable<T> for a keyed service request, AnyKey ("*")
registrations should not appear in the results. Two cases fixed in
CreateEmitMethodForEnumerableServiceServiceRequest (both variance and
no-variance branches):

- GetKeyedServices(AnyKey): exclude services registered under "*" itself
- GetKeyedServices("some-key"): do not pull in "*" registrations as fallback

Single-service resolution with AnyKey wildcard fallback is unaffected.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@seesharper seesharper merged commit 2fd3438 into master May 21, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant