Exclude keyed registrations from IEnumerable<T> / T[] resolution#430
Merged
jeremydmiller merged 1 commit intoMay 29, 2026
Merged
Conversation
MS DI's `IServiceProvider.GetServices(T)` and `T[]` resolution return only non-keyed registrations; keyed registrations are looked up explicitly via `GetKeyedService(T, key)`. Lamar's `ListInstance` / `ArrayInstance` populated their `Elements` straight from `ServiceGraph.FindAll`, which is built from `ServiceFamily.All` — the flat union of keyed and non-keyed instances for a given service type. As a result a keyed registration of `T` ended up as an `IEnumerable<T>` element, deviating from MS DI semantics and, more importantly, causing infinite recursion for code-generating consumers: JasperFx's `EnumerableSingletons.KeyedMirror` registers a keyed Singleton lambda whose factory calls `sp.GetServices(elementType)` to return the shared non-keyed singleton instance. When Lamar inlines the mirror's Singleton via `InjectedServiceField.ToVariableExpression`'s `QuickResolve`, the factory invokes `sp.GetServices(T)`, Lamar resolves the `IEnumerable<T>` family (still including the mirror itself), the generated build frame inlines the mirror again, and so on — ~750-frame stack overflow before any handler runs. Filter `IsKeyedService` out of `createPlan` in both `ListInstance` and `ArrayInstance`, matching MS DI's behaviour and breaking the cycle. Explicit keyed lookups (`GetKeyedService(T, key)`) are unaffected. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
IServiceProvider.GetServices(T)andT[]resolution in MS DI returnonly non-keyed registrations; keyed registrations are looked up
explicitly via
GetKeyedService(T, key).Lamar's
ListInstance/ArrayInstancebuilt theirElementsstraightfrom
ServiceGraph.FindAll(T), which is sourced fromServiceFamily.All— the flat union of keyed and non-keyed instancesfor a given service type. As a result a keyed registration of
Tended up in the materialised
IEnumerable<T>/T[], deviating fromMS DI semantics.
The same divergence is the load-bearing cause of a hard
stack overflow with JasperFx 2.2.x's
AddJasperFxEnumerableSingletonSupport():EnumerableSingletons.KeyedMirrorregisters a keyed Singleton lambdawhose factory calls
sp.GetServices(elementType)to return the sharednon-keyed singleton instance. When Lamar inlines the mirror's Singleton
via
InjectedServiceField.ToVariableExpression'sQuickResolve, thefactory invokes
sp.GetServices(T), Lamar resolves theIEnumerable<T>family (still including the mirror itself), thegenerated build frame inlines the mirror again, and so on — ~750-frame
stack overflow before any handler runs.
Filtering
IsKeyedServiceout ofcreatePlanin bothListInstanceand
ArrayInstancematches MS DI's behaviour and breaks the cycle.Explicit keyed lookups (
GetKeyedService(T, key)) are unaffected.Test plan
Lamar.Testing— 709/709 pass locally (net8 + net9 + net10).Lamar.AspNetCoreTests— 23/23 pass (net8/9/10).OpenApiKeyedServiceIntegrationTests— 4/4 pass.MinimalApiTests— 2/2 pass.Wolverine 6.2.1 that previously stack-overflowed in
ListInstance.CreateBuildFrame→EnumerableSingletons.KeyedMirror.b__0now starts cleanly andruns an end-to-end handler invocation.
🤖 Generated with Claude Code