Commit 73aa9f6
Exclude keyed registrations from IEnumerable<T> / T[] resolution
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>1 parent 372b588 commit 73aa9f6
2 files changed
Lines changed: 14 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
66 | | - | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
67 | 73 | | |
68 | 74 | | |
69 | 75 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
59 | | - | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
60 | 66 | | |
61 | 67 | | |
62 | 68 | | |
| |||
0 commit comments