Repository navigation
Description
Description
The reflection-based and source-generated configuration binders handle some collection elements differently. Enabling source-generated binding changes both the resulting collection and whether provider exceptions propagate.
Reproduction steps
Run this console application referencing Microsoft.Extensions.Configuration.Binder, first with configuration binding generation disabled, then enabled:
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Configuration;
var numbers = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?>
{
["Numbers:0:Ignored"] = "x",
["Numbers:1"] = "7",
})
.Build();
Show("Numbers", () => numbers.GetSection("Numbers").Get<int[]>());
var names = new ConfigurationRoot(
new List<IConfigurationProvider> { new ThrowingProvider() });
Show("Names", () => names.GetSection("Names").Get<string[]>());
static void Show<T>(string name, Func<T[]?> bind)
{
try
{
T[]? values = bind();
string result = values is null ? "null" : $"[{string.Join(", ", values)}]";
Console.WriteLine($"{name}: {result}");
}
catch (Exception exception)
{
Console.WriteLine(
$"{name}: {exception.GetType().Name}: {exception.Message}");
}
}
sealed class ThrowingProvider : ConfigurationProvider
{
public ThrowingProvider()
{
Set("Names:0", "unused");
Set("Names:1", "good");
}
public override bool TryGet(string key, out string? value)
{
if (string.Equals(key, "Names:0", StringComparison.OrdinalIgnoreCase))
{
throw new InvalidOperationException("Provider read failed.");
}
return base.TryGet(key, out value);
}
}
Actual behavior
With the default BinderOptions:
| Scenario | Reflection binder | Source-generated binder |
|---|---|---|
| Numeric collection element has children but no scalar value | [0, 7] |
[7] |
| Provider throws while reading a string collection element | [good] |
Throws InvalidOperationException |
In the first case, reflection binding creates a default-valued element, while generated binding skips it.
In the second case, reflection binding suppresses the provider exception and skips the element, while generated binding propagates the exception.
Expected behavior
Both binders should apply the same collection-element and exception-handling policies. This report does not assume either binder's current behavior is necessarily the correct policy.
Notes
These two bugs are not closely related, so feel free to fix each separately. Or I could file separate issues for each of them.
Related issues
- Binding of empty string is inconsistent between reflection and source generator configuration binders #132968 covers other binder inconsistencies involving empty strings.
- Configuration binding does not throw when it should #110147 covers different exception-handling cases involving
ErrorOnUnknownConfiguration.
Note
This issue was drafted with GitHub Copilot.
Activity
dotnet-policy-service commentedon Nov 4, 2026
Tagging subscribers to this area: @dotnet/area-extensions-configuration
See info in area-owners.md if you want to be subscribed.
Hack15951 commentedon Nov 24, 2026
Hi @rzikm / @dotnet/area-extensions-configuration — I see this is in the 12.0.0 milestone. Is it still open for external contribution? I'd like to take it and add a third boundary case (provider throwing mid-collection) if nobody is working on it.
mrek-msft commentedon Nov 24, 2026
@Hack15951 Yes, it is open for external contributors. cc @svick for evaluation if this is desired approach and if breaking change process needs to be followed.
Hack15951 commentedon Nov 24, 2026
Thanks @mrek-msft. @svick — before I start, could you confirm which behavior should be the baseline? The issue doesn't assume either binder is correct: should the source-generated binder be changed to match reflection (skip elements without scalar values / suppress provider exceptions), or should both be aligned to a new third behavior? Also, if this counts as a breaking change for 12.0, could you point me to the process to follow? I'll prepare the repro and a third boundary case (provider throwing mid-collection) in the meantime.
svick commentedon Nov 24, 2026
@Hack15951 Thanks. In this case, I think returning only valid valid values for the first case and rethrowing the exception for the second case are the better behaviors.
Throwing an exception in a situation that didn't throw before is usually considered a breaking change, so I'm marking the issue as such. The breaking change process only happens after the PR is merged and is usually done by us, so you don't need to do anything.
dotnet-policy-service commentedon Nov 24, 2026
Added
needs-breaking-change-doc-createdlabel because this issue has thebreaking-changelabel.needs-breaking-change-doc-createdlabel.Tagging @dotnet/compat for awareness of the breaking change.
Hack15951 commentedon Nov 25, 2026
Thanks @svick — clear. I'll align the source-generated binder to return only valid values for case 1 and the reflection binder to rethrow for case 2, with a regression test covering the provider-throwing-mid-collection case. Understood that the breaking-change doc is handled by the team after merge.