C#C
C#16mo ago
Core

✅ .NET Worker service doesn't load appsettings.json into a class

Hello,
I am trying to load configuration values into a class or record, but it doesn't work. The properties remain null.

I do the following:
c#
builder.Services.Configure<DnsRecordOptions>(builder.Configuration.GetSection("DnsRecords"));


c#
"DnsRecords:": {
  "ARecord": "0.0.0.0",
},


c#
public record DnsRecordOptions
{
    public required string ARecord { get; init; }
}

Loading config values this way works in a Web API project, but I don't understand why it wouldn't in an other type of project.
I've also added the DnsRecords configuration to appsettings.json and appsetting.Development.json, but the outcome was the same.
image.png
Was this page helpful?