"Null values preserved in configuration": source generator and in-memory provider statements don't match .NET 9.0.22 / 10.0.14 behavior

未关闭 适合新手
#56,789 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
1/5
预计耗时
1 小时以内
新手友好度
86/100
Issue 类型
文档
描述清晰度
描述清楚
活跃度
活跃
技术栈
csharp
领域
documentation

调研方向

阅读链接的兼容性页面,尤其是“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 null value, such as the in-memory provider, already bound null to 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

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

dotnet/docs 的其他 Issue

查看 dotnet/docs 的全部 Issue

相似的 Issue

更多 Documentation Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。