Repository navigation
Description
Description
For a public getter-only collection property, the configuration binding source generator emits code that unconditionally allocates a new collection instance (??= new List<>()), populates it via BindCore, and then discards it — because there is no setter to write it back to. This is wasted allocation and binding work at runtime with no observable effect.
The runtime reflection binder does not do this: it explicitly short-circuits when a read-only binding point has a null value and allocates nothing.
This is a performance/parity gap, not a correctness bug — the binding result is identical (nothing is observably bound in either case).
Reproduction Steps
Enable the source generator and bind a type with a public getter-only collection property that returns null:
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>public class ExampleOptions
{
public List<string> UsesCollection { get; } // getter-only, backing value null
}
services.Configure<ExampleOptions>(config.GetSection("Repro"));If the "Repro" section has children, the generated BindCore runs the allocate-populate-discard sequence below.
Generated code (simplified)
// in BindCore for ExampleOptions
List<string>? temp = instance.UsesCollection; // null
temp ??= new List<string>(); // allocates a list...
BindCore(section, ref temp, defaultValueIfNotFound: false, binderOptions); // ...and populates it
// temp is never written back (no setter) -> discardedExpected behavior
When a property has a getter but no usable setter (canSet == false) and the getter returns null, the generator should not allocate or bind a throwaway instance — matching the reflection binder, which returns early:
// ConfigurationBinder.cs (reflection binder)
if (bindingPoint.Value is null)
{
// if the binding point doesn't let us set a new instance, there's nothing more we can do
if (bindingPoint.IsReadOnly)
return;
...
}Actual behavior
The generator allocates a new collection, fully binds it from configuration, and then throws it away.
Root cause
In CoreBindingHelpers.EmitBindingLogicForComplexMember, the initKind for a reference-type member is selected from member.CanGet alone, ignoring canSet:
else if (member.CanGet)
{
targetObjAccessExpr = memberAccessExpr;
initKind = InitializationKind.AssignmentWithNullCheck; // emits `temp ??= new ...()`
}The write-back is suppressed separately (writeOnSuccess = !canSet ? null : ...), so for a getter-only property the allocation and BindCore still execute but the result is discarded. A guard that skips the ??=/BindCore when !canSet and the current value is null (e.g. wrapping in if (temp is not null)) would bring it in line with the reflection binder.
- Source generator:
src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs(EmitBindingLogicForComplexMember, getter branch +writeOnSuccess) - Reflection binder short-circuit:
src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs(BindInstance,if (bindingPoint.IsReadOnly) return;)
Related
- Configuration Binder source generator can generate code that won't compile #111517 (getter-only value-tuple collection that surfaced this code path)
- Fix config binder source generator emitting invalid tuple instantiation code #130119 (removes dead
BindCorehelpers; does not address this runtime allocation)
Note
This issue was drafted with the assistance of GitHub Copilot.
Activity
dotnet-policy-service commentedon Jul 16, 2026
Tagging subscribers to this area: @dotnet/area-extensions-configuration
See info in area-owners.md if you want to be subscribed.
Suriya987 commentedon Jul 19, 2026
I'd like to work on this — planning to add the canSet-aware guard in EmitBindingLogicForComplexMember as described. Will look for the existing source-gen test project to add a regression test
Sahithbasani commentedon Jul 19, 2026
Hi @svick, I implemented the proposed fix in #131045.
The source generator now checks the current value of a getter-only property and only emits the binding logic when that value is non-null. I also added a regression baseline covering both a null getter-only collection and an initialized getter-only collection.
The targeted tests and the full SourceGenerationTests passed for both net11.0 and net472.
Could you please review the approach when you have a chance? Thank you.
svick commentedon Jul 20, 2026
@Suriya987 Sorry, it looks like @Sahithbasani was faster.
Suriya987 commentedon Jul 20, 2026
@svick may be yah but , I will do for upcoming issues as fast as possible
svick commentedon Jul 21, 2026
@Suriya987 There are plenty of existing open issues, those have a much smaller chance of a race. 😄
ConfigurationBinder: Skip binding null getter-only collection propert…