Description
The ConfigurationBinder source generator in the AOT template generates code that doesn’t consider the use of nullable value types like int, long, etc. As a result, binding fails with an InvalidOperationException because it tries to parse an empty string as an int in generated code.
Microsoft.Extensions.Configuration.Binder.SourceGeneration version 8.0.10.26715
Reproduction Steps
Configuration class:
public class LimitSetup
{
public int? MaxCount { get; set; }
}
Binding in Program.cs (ASP web api AOT template):
builder.Services
.AddOptions<LimitSetup>()
.BindConfiguration("LimitSetup");
In appsettings.json:
"LimitSetup": {
"MaxCount": null
}
csproj file:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization>
<PublishAot>true</PublishAot>
</PropertyGroup>
</Project>
Expected behavior
In options LimitSetup.MaxCount property is null value.
Actual behavior
I get an exception when binding:
System.InvalidOperationException: 'Failed to convert configuration value at 'LimitSetup:MaxCount' to type 'System.Int32'.'
ConfigurationBinder source generator generate this code:
if (configuration["MaxCount"] is string value1)
{
instance.MaxCount = ParseInt(value1, () => configuration.GetSection("MaxCount").Path);
}
Value value1 is empty string if MaxCount is null in appsettings.json.
Regression?
No response
Known Workarounds
No response
Configuration
.NET 8 - ASP.NET Core Web API (Native AOT) template
Other information
No response
Description
The ConfigurationBinder source generator in the AOT template generates code that doesn’t consider the use of nullable value types like
int,long, etc. As a result, binding fails with anInvalidOperationExceptionbecause it tries to parse an empty string as anintin generated code.Microsoft.Extensions.Configuration.Binder.SourceGenerationversion8.0.10.26715Reproduction Steps
Configuration class:
Binding in Program.cs (ASP web api AOT template):
In appsettings.json:
csproj file:
Expected behavior
In options
LimitSetup.MaxCountproperty isnullvalue.Actual behavior
I get an exception when binding:
ConfigurationBinder source generator generate this code:
Value
value1is empty string ifMaxCountisnullin appsettings.json.Regression?
No response
Known Workarounds
No response
Configuration
.NET 8 - ASP.NET Core Web API (Native AOT) template
Other information
No response