Open
Description
Description
When binding root-level configuration properties using Microsoft.Extensions.Configuration.Binder, binding to a record defined with a positional parameter decorated with [ConfigurationKeyName] fails to bind the expected value (throws Exception), while binding to a class with an attributed property works as expected. The following minimal reproducible example demonstrates the issue.
Reproduction Steps
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Configuration;
#nullable disable
var inMemorySettings = new Dictionary<string, string>
{
{ "example-secret-key", "my-secret-value" }
};
IConfiguration configuration = new ConfigurationBuilder()
.AddInMemoryCollection(inMemorySettings)
.Build();
var secretsClass = configuration.Get<ExampleSecretsClass>();
Console.WriteLine("ExampleSecretsClass.SecretKey: " + (secretsClass?.SecretKey ?? "null"));
var secretsRecord = configuration.Get<ExampleSecretsRecord>();
Console.WriteLine("ExampleSecretsRecord.SecretKey: " + (secretsRecord?.SecretKey ?? "null"));
public class ExampleSecretsClass
{
[ConfigurationKeyName("example-secret-key")]
public string SecretKey { get; init; } = string.Empty;
}
public record ExampleSecretsRecord([property: ConfigurationKeyName("example-secret-key")] string SecretKey);
Expected behavior
When using constructor binding with ConfigurationKeyName, the binding fails and an Exception is thrown
Actual behavior
It should bind even when constructor binding is used
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
No response