React to configuration changes in .NET

OptionsMonitor
is a powerful tool that allows you to dynamically monitor configuration options and react to changes.

In this example, we consider the Starfleet's warp drive, which will be commissioned a few years after 2063. Undoubtedly, the system must be informed if the chief engineer has made some changes to the operating parameters.
warpcore.json
{
    "WarpCore":
    {
        "DilithiumFractionRatio": 0.456,
        "MaximumSafeWarpFactor": 9.99656,
        "CanTransWarp": false
    }
}
WarpCoreOptions.cs
public record WarpCoreOptions
{
    public Double DilithiumFractionRatio { get; init; }
    public Double MaximumSafeWarpFactor { get; init; }
    public Boolean CanTransWarp { get; init; }
}

Request
IOptionsMonitor
from the DI container.

Instead of using
IOptions
rom the DI container, one should alternatively request
IOptionsMonitor
. This interface provides the capability to register a delegate as soon as the configuration source (often a JSON file) changes.
MarkSixWarpCore.cs
public class MarkSixWarpCore : IWarpCore, IDisposable
{
    private readonly IDisposable? _OptionsChangedListener;

    public MarkSixWarpCore(IOptionsMonitor<WarpCoreOptions> options)
    {
        _OptionsChangedListener = options.OnChange(OnOptionsChanged);
        ConfigureWarpCore(options.CurrentValue);
    }

    private void OnOptionsChanged(WarpCoreOptions options)
    {
        ConfigureWarpCore(options);
    }

    private void ConfigureWarpCore(WarpCoreOptions options)
    {
        SetTransWarpCapability(options.CanTransWarp);
        ...
    }

    /// <inheritdoc />
    public void Dispose()
    {
        _OptionsChangedListener?.Dispose();
    }
}

Add the JSON file to the
IConfigurationBuilder
.

The JSON file is registered, specifying
reloadOnChange
to indicate that the file should be reloaded when changes are detected in the file system.
Program.cs
private static IHostBuilder CreateHostBuilder(String[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureHostConfiguration(configHost =>
        {
            configHost.AddJsonFile("warpcore.json", reloadOnChange: true);
        });

A glue to bind them all

In addition to setting up the options class using the
ConfigureOptions
class, which I examine in more detail in this article , an
IOptionsChangeTokenSource
must also be registered. The
ConfigurationChangeTokenSource
implementation is suitable for this purpose, as it handles the notifications.
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
    services.TryAddEnumerable(
        ServiceDescriptor.Singleton<IConfigureOptions<WarpOptions>, WarpOptionsSetup>());

    services.AddSingleton<IOptionsChangeTokenSource<TOptions>>(serviceProvider =>
    {
        var configuration = serviceProvider.GetRequiredService<IConfiguration>();
        return new ConfigurationChangeTokenSource<TOptions>(name, configuration);
    });
}

Share the post

Read configuration data in .NET

Read configuration data in .NET

Automatic parsing of configuration data from JSON into a DTO using the

ConfigureOptions
class.