0

What would be the best way to combine and bind variables from both appsettings and environment variables into one model? Using dotnet7

E.g.

public class MyOptions
{
    public const string SectionName = "MySettings";

    // this property should be set from appsettings.json
    public string Address { get; set; }

    // this property should be set from environment variable
    public int TestNumber { get; set; }
]

Then in program.cs

builder.Services
    .AddOptions<MyOptions>()
    .Bind(config.GetSection(MyOptions.SectionName))
    .ValidateOnStart();

Will bind the part from appsettings section but how can I solve the environment variable. It works by seperating them into two different models and "duplicate" the AddOptions but would rather keep it into one model rather than two.

As mention, seperating them into two different models works but haven't been able to figure out the way to combine them.

Fairly new to csharp so be gentle :)

FreKac
  • 13
  • 3
  • Given "`builder.Services`", you're using a .NET 6+ web application builder. That means that both configuration sources are added for you, and the values from both are merged before binding occurs. As long as you specify each where you want it, what you want will happen automatically. – madreflection Mar 24 '23 at 14:41
  • Related: https://stackoverflow.com/questions/75727580/trying-to-alter-net-6-configuration-class-in-configureservices – madreflection Mar 24 '23 at 14:45
  • In the example above I specify which sectionname to look for in appsettings and it works fine to bind it that way. But then the TestNumber property will be null since it's in an environment variable and not part of the GetSection. – FreKac Mar 24 '23 at 14:52
  • What did you name your environment variable? – madreflection Mar 24 '23 at 14:54
  • *"the TestNumber property will be null"* - It can't be null because it's `int`, not `int?`. – madreflection Mar 24 '23 at 14:56
  • The environment variable is TestNumber, trying to keep it simple while testing/developing so I keep the env variable and property name the same. – FreKac Mar 24 '23 at 14:57
  • It should be named `MySettings:TestNumber`. The name needs to match the property path. Please read the linked answer. It explains how that works. – madreflection Mar 24 '23 at 14:57
  • For the sake of argument, it's the same problem if I replace it with "public string TestString {get;set;}" – FreKac Mar 24 '23 at 15:00
  • I'll check the link – FreKac Mar 24 '23 at 15:01
  • Thanks madreflection, renaming the env variable solved it, didn't know section would also map to an environment variable "prefix". – FreKac Mar 24 '23 at 15:10
  • Don't think about it as a prefix; that's going to hinder your understanding. A prefix is a special-purpose thing, which is not the case here. The "MySettings" part is just the first property in a sequence of properties that need to be traversed. Both parts serve the same purpose, in sequence. and together form the full *property path*. If you think of it as a prefix, the next time you'll be asking yourself "Do I need a prefix?" which is the wrong question. What you should be asking yourself is, "What's the full property path?" It's better to understand it in terms of that from the start. – madreflection Mar 24 '23 at 16:27

0 Answers0