.NET Core Options Pattern For Live Reload Settings

There are many great posts about implementing the Options Pattern .NET Core with IOptionsMonitor on the interwebs, but many fail to point out one “gotcha” when working with IOtionsMonitor to live reload settings.

There are different ways to load settings into the dependency injection service container, but the way I like to do it is as follows:

public record MySettings 
{
  [Required] public string Setting1 { get; init; }
  [Required] [Range(1, int.MaxValue)] public int Setting2 { get; init; }
}

Notice the data annotations, with the Options Pattern. It is possible to validate settings before they are even injected into your services.

Below is how you bind and inject the settings into the dependency injection service container

serviceCollection
  .AddOptions<MySettings>()
  .Bind(configuration.GetSection(nameof(MySettings)))
  .ValidateDataAnnotations()
  .ValidateOnStart();

Two important things to note here

  • ValidateDataAnnotations() – As the name implies, this will use the data annotations on the record’s or class’ properties to validate the settings. You will need Microsoft.Extensions.Options.DataAnnotations NuGet package for this to work.
  • ValidateOnStart() – Runs validations eagerly, when the app starts. Otherwise, the validations will run when the properties are accessed on whatever service you are injecting those properties.

Remember I mentioned there is a “gotcha“?

If you are explicitly adding the JSON configuration files, there is an optional boolean argument that will need to be set to true: reloadOnChange.

builder.AddJsonFile("appsettings.json", reloadOnChange: true);

Without reloadOnChange set to true, live reloading of the settings will not work.

Now, injecting the settings to enable live reloading using IOtionsMonitor<TOptions>

public sealed MyClass
{
    IOptionsMonitor<MySettings> _mySettingsDelegate;

    public MyClass
    (
        IOptionsMonitor<MySettings> mySettingsDelegate
    )
    {
        _mySettingsDelegate = mySettingsDelegate;
    }

    public void DoSomething()
    {
        var setting1 = _mySettingsDelegate.CurrentValue.Setting1;
        var setting2 = _mySettingsDelegate.CurrentValue.Setting2;
    }
}

IOptionsMonitor is registered as a singleton and can be injected into any service lifetime. Upon appsettings.json changing, the service will get the new values when it is called.

To learn more about IOptionsMonitor and the other Options Interfaces, head over to the Microsoft Documentation: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-7.0

If you liked this tutorial, share it on your social media and you can follow me on Twitter or LinkedIn.


Consider giving back by getting me a coffee (or a couple) by clicking the following button:

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.