I would like to bind configuration to record type.
This is definition of configuration type (it is without parameterless constructor):
public record AppConfiguration(string ConnectionString);
This is sample Main
method:
public static void Main(string[] args)
{
var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
AppConfiguration appConfig = new(); // error
configuration.GetSection("app").Bind(appConfig);
}
If I convert definition to this:
public record AppConfiguration
{
public string ConnectionString {get; init;}
}
it works as expected, but I would rather use "single line" definition of the record.
Are records right way for this use case?
question from:
https://stackoverflow.com/questions/65859447/bind-iconfiguration-to-c-sharp-record-type 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…