The asp.net configuration system allows you to pull in configuration settings from multiple places, have them bound at runtime to a type of your choosing, and injected into your services and types at runtime.
However, settings might not always be available, perhaps due to a misconfigration.
If we have the following options type:
public record class ExampleSetting(string? StringSetting, int? IntSetting);
We can use the helper methods defined in OptionsExtensions
to get any setting that is
required, or throw an MissingOptionsValueException
exception like this:
public class MyService
{
public MyService(IOptions<ExampleSetting> options)
{
string stringSetting = options.GetRequiredValue(x => x.StringSetting);
int intSetting = options.GetRequiredValue(x => x.IntSetting);
}
}