ContainerConfigurationMonitor 0.1.2

dotnet add package ContainerConfigurationMonitor --version 0.1.2                
NuGet\Install-Package ContainerConfigurationMonitor -Version 0.1.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ContainerConfigurationMonitor" Version="0.1.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ContainerConfigurationMonitor --version 0.1.2                
#r "nuget: ContainerConfigurationMonitor, 0.1.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ContainerConfigurationMonitor as a Cake Addin
#addin nuget:?package=ContainerConfigurationMonitor&version=0.1.2

// Install ContainerConfigurationMonitor as a Cake Tool
#tool nuget:?package=ContainerConfigurationMonitor&version=0.1.2                

ContainerConfigurationMonitor

Build Status

A .NET library for monitoring configuration file changes in containers.

Features

  • Monitors configuration file changes using IContainerFileWatcher.
  • Reloads configuration upon file changes.
  • Debounces rapid configuration changes to prevent multiple reloads.
  • Integrates with IOptionsMonitor to handle configuration changes.
  • Includes logging support with ILogger.

Installation

You can install the ContainerConfigurationMonitorService via NuGet.

  1. Add the package to your project :

    dotnet add package ContainerConfigurationMonitorService
    

Or add the package with Package Manager

Install-Package ContainerConfigurationMonitorService
  1. Register the ContainerConfigurationMonitorService in your Startup.cs or Program.cs:

    using ContainerConfigurationMonitor;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;
    
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }
    
        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((context, config) =>
                {
                    config.SetBasePath(context.HostingEnvironment.ContentRootPath)
                          .AddJsonFile("Config/appsettings.json", optional: false, reloadOnChange: false);
                })
                .ConfigureServices((context, services) =>
                {
                    services.Configure<AppSettings>(context.Configuration.GetSection("Settings"));
                    services.AddContainerConfigurationMonitorService("Config/appsettings.json");
                    services.AddHostedService<MyService>();
                });
    }
    

Usage

Registering the Service

To use the ContainerConfigurationMonitorService, register it with the dependency injection container:

using ContainerConfigurationMonitor;
using Microsoft.Extensions.DependencyInjection;

public void ConfigureServices(IServiceCollection services)
{
    services.AddContainerConfigurationMonitorService("Config/appsettings.json");
}

Using Debounced OnChange with IOptionsMonitor

You can use the OnChangeDebouncer extension method to debounce the OnChange event of IOptionsMonitor:

using Microsoft.Extensions.Options;
using System;
using System.Threading;
using System.Threading.Tasks;

public class MyService : IHostedService
{
    private readonly IOptionsMonitor<AppSettings> _optionsMonitor;
    private AppSettings _currentSettings;
    private IDisposable _changeListener;

    public MyService(IOptionsMonitor<AppSettings> optionsMonitor)
    {
        _optionsMonitor = optionsMonitor;
        _currentSettings = _optionsMonitor.CurrentValue;

        // Register for changes with debounce
        _changeListener = _optionsMonitor.OnChangeDebouncer(OnSettingsChanged, TimeSpan.FromMilliseconds(100));
    }

    private void OnSettingsChanged(AppSettings newSettings)
    {
        _currentSettings = newSettings;
        Console.WriteLine($"Settings changed: Setting1={_currentSettings.Setting1}, Setting2={_currentSettings.Setting2}");
    }

    public Task StartAsync(CancellationToken cancellationToken)
    {
        Console.WriteLine("MyService started.");
        Console.WriteLine($"Initial settings: Setting1={_currentSettings.Setting1}, Setting2={_currentSettings.Setting2}");
        return Task.CompletedTask;
    }

    public Task StopAsync(CancellationToken cancellationToken)
    {
        Console.WriteLine("MyService stopped.");
        _changeListener.Dispose();
        return Task.CompletedTask;
    }
}

Sample Configuration

Ensure your appsettings.json file is correctly set up in the Config directory:

{
    "Settings": {
        "Setting1": "Value1",
        "Setting2": "Value2"
    }
}

Logging

The ContainerConfigurationMonitorService supports logging via ILogger. Ensure you have logging configured in your application to capture logs from the service:

using Microsoft.Extensions.Logging;

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureLogging(logging =>
        {
            logging.ClearProviders();
            logging.AddConsole();
        })
        .ConfigureServices((context, services) =>
        {
            // Your service registrations
        });

MIT License

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.1.2 63 7/28/2024