Mitch
Mitch

Reputation: 22251

Change in behavior of Microsoft.Extensions.Configuration defaults when using `PublishAOT`

Why when using PublishAOT does the default value of value-type for a typed configuration class become zero?

Example:

var builder = new ConfigurationBuilder();

// Not neccessary for repro, but to reduce confounding variables
builder.Sources.Clear();
builder.AddInMemoryCollection(new Dictionary<string, string?>()
{
    ["String1"] = "Override1",
});

var config = builder.Build().Get<TypedConfig>();
Console.WriteLine($"String1: {config?.String1}");
Console.WriteLine($"String2: {config?.String2}");
Console.WriteLine($"Int3: {config?.Int3}");
Console.WriteLine($"Bool4: {config?.Bool4}");

class TypedConfig
{
    public string String1 { get; set; } = "Value1";
    public string String2 { get; set; } = "Value2";
    public int Int3 { get; set; } = 443;
    public bool Bool4 { get; set; } = true;
}

Output when built with dotnet build:

String1: Override1
String2: Value2
Int3: 443
Bool4: True

Output when built with dotnet publish -c Release -r win-x64 -p:PublishAot=true using .Net 8.0.300:

String1: Override1
String2: Value2
Int3: 0
Bool4: False

Upvotes: 1

Views: 40

Answers (1)

Mitch
Mitch

Reputation: 22251

Seems to be a bug in EnableConfigurationBindingGenerator=true, which is enabled by default with PublishAOT=true. There's an open issue in the runtime repo.

Upvotes: 0

Related Questions