© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•14mo ago•
24 replies
sl8er

Overriding appsettings.json section with environment variable

Given a configuration section called
Tenants
Tenants
, I'd like to override it with a whole JSON structure via an environment variable.

Given I want this
"Tenants": {
  "ByCode": {
    "11": {
      "Name": "acme"
    },
    "42": {
      "Name": "silver"
    }
  }
}
"Tenants": {
  "ByCode": {
    "11": {
      "Name": "acme"
    },
    "42": {
      "Name": "silver"
    }
  }
}


I'm doing
export Tenants='{"ByCode": {"11": {"Name": "acme"}, "42": {"Name": "silver"}}}'
export Tenants='{"ByCode": {"11": {"Name": "acme"}, "42": {"Name": "silver"}}}'


I'm trying to bind it against an object with the options pattern:
builder.Services.Configure<TenantsOptions>(
        builder.Configuration.GetSection(TenantsOptions.SectionName));
builder.Services.Configure<TenantsOptions>(
        builder.Configuration.GetSection(TenantsOptions.SectionName));


And the classes look like:
public class TenantsOptions
{
    public const string SectionName = "Tenants";

    public Dictionary<string, Tenant> ByCode { get; set; } = new();
}

public class Tenant
{
    public string Name { get; set; }
}
public class TenantsOptions
{
    public const string SectionName = "Tenants";

    public Dictionary<string, Tenant> ByCode { get; set; } = new();
}

public class Tenant
{
    public string Name { get; set; }
}


When I run the ASP.NET Core app, the
ByCode
ByCode
dictionary is empty. But if I just paste the JSON section in appsettings.json, it's correctly populated.

For the record, I can see the whole structure if I just log
builder.Configuration.GetSection(TenantsOptions.SectionName).Value;
builder.Configuration.GetSection(TenantsOptions.SectionName).Value;
, but for some reason, I guess it's only interpreted as a string, and not correctly deserialized and bound to
TenantOptions
TenantOptions
?

So how do you actually override a whole section with a JSON value through an environment variable?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Check if appsettings.json or appsettings.Development.json
C#CC# / help
3y ago
Appsettings.json across projet
C#CC# / help
2y ago
✅ How to edit and save specific appsettings.json (project root) section?
C#CC# / help
4y ago
Correct environments handling (appSettings.json)
C#CC# / help
3y ago