"Null values preserved in configuration": source generator and in-memory provider statements don't match .NET 9.0.22 / 10.0.14 behavior
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 1/5
- 预计耗时
- 1 小时以内
- 新手友好度
- 86/100
- Issue 类型
- 文档
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- csharp
调研方向
阅读链接的兼容性页面,尤其是“Non-nullable value types”部分,并将该陈述与 issue 中的复现示例和生成代码进行比较。修正该陈述,说明 reflection binder 和 source generator 在何种情况下会为 null 或空的配置值保留初始值。页面准确说明所报告的 .NET 9 和 .NET 10 行为后,即为完成。
由索引模型根据 Issue 内容生成。
描述
Page: https://learn.microsoft.com/dotnet/core/compatibility/extensions/10.0/configuration-null-values-preserved (read 2026-11-08)
Statement on the page (section "Non-nullable value types"):
Providers that store a real
nullvalue, such as the in-memory provider, already boundnullto non-nullable value-type properties by setting the default value, even before .NET 10, and the configuration source generator also matches this behavior.
What we observe: neither part holds when the property has an initial value.
Repro (Microsoft.NET.Sdk.Web, TargetFrameworks net9.0;net10.0, built once with EnableConfigurationBindingGenerator false and once with true):
using Microsoft.Extensions.Configuration;
var config = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?>
{
["Settings:Name"] = "x",
["Settings:TimeoutSeconds"] = null,
})
.Build();
Settings settings = config.GetSection("Settings").Get<Settings>()!;
Console.WriteLine($"{Environment.Version} TimeoutSeconds = {settings.TimeoutSeconds}");
public sealed class Settings
{
public string? Name { get; set; }
public int TimeoutSeconds { get; set; } = 30;
}
Output (SDK 10.0.403, Windows):
| Binder | .NET 9.0.22 | .NET 10.0.14 |
|---|---|---|
| Reflection | 30 | 0 |
| Source generator | 30 | 30 |
- With the in-memory provider, the reflection binder on .NET 9 kept the initial value (30); it did not set
default(int). - The source generator keeps the initial value on both versions. The generated code assigns a non-nullable value-type property only when the value is neither null nor empty, for example (.NET 10.0.14 generator output):
if (TryGetConfigurationValue(configuration, key: "TimeoutSeconds", out string? value6))
{
if (!string.IsNullOrEmpty(value6))
{
instance.TimeoutSeconds = ParseInt(value6, configuration.GetSection("TimeoutSeconds").Path);
}
}
The same holds for a JSON null (AddJsonFile): reflection binder 0 on .NET 10, source generator 30 on .NET 9 and .NET 10. So on .NET 10 the same configuration produces different values depending on whether the generator is enabled, which matters for apps where it is on by default (AOT web apps, PublishTrimmed).
Suggested fix: remove or correct the sentence, and mention that the source generator does not bind null (or "") to non-nullable value-type properties, so the initial value is kept.
- 主要语言
- 没有语言数据
- 星标
- 4.8k
- 派生
- 6.1k
- 平均合并
- 1 天 1 小时
- 30 天内合并 PR
- 305
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
dotnet/docs 的其他 Issue
-
Update the .NET Framework 3.5 on Windows 11 FAQ to mention Windows 11 26H2可能已有人在做 @BillWagner 今天认领。 未关闭:world_map: reQUEST dotnet/svc
难度 1/5 1 小时以内 新手友好度 78/100
dotnet/docs#56791 · 1 个 reaction · 已指派 2 人 ·
维护者通常 1 天内回复
-
Improve description of `limitations of extensions`可能已有人在做 @Manish-1kumar 于 9 天前认领。 未关闭advanced-concepts/subsvc dotnet-csharp/svc
难度 1/5 1-3 小时 新手友好度 84/100
维护者通常 1 天内回复
-
dg未关闭dotnet-fundamentals/svc needs-more-info
难度 2/5 1-3 小时 新手友好度 67/100
维护者通常 1 天内回复
-
Document unit-test / theory-data impact of .NET 11 double→decimal conversion可能已有人在做 @SkyDevLab 于 19 天前认领。 未关闭:watch: Not Triaged dotnet-fundamentals/svc
难度 1/5 1-3 小时 新手友好度 88/100
维护者通常 1 天内回复
-
dotnet-fundamentals/svc install/subsvc
难度 2/5 1-3 小时 新手友好度 72/100
dotnet/docs#56344 · 1 条评论 · 1 个 reaction ·
维护者通常 1 天内回复
相似的 Issue
-
难度 1/5 1 小时以内 新手友好度 67/100
MicrosoftDocs/WSL#2654 ·
-
难度 1/5 1-3 小时 新手友好度 85/100
hibernate/hibernate-reactive#4485 ·
维护者通常 1 天内回复
-
vuln:LOW
难度 1/5 1 小时以内 新手友好度 63/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 66/100
mxschmitt/awesome-playwright#285 ·
维护者通常 19 天内回复
-
area/access-management Frontend onboarding candidate :handshake:
难度 2/5 1-3 小时 新手友好度 65/100
Altinn/altinn-auth#4941 ·
维护者通常 1 天内回复