ContainerConfigurationMonitor 0.1.2
dotnet add package ContainerConfigurationMonitor --version 0.1.2
NuGet\Install-Package ContainerConfigurationMonitor -Version 0.1.2
<PackageReference Include="ContainerConfigurationMonitor" Version="0.1.2" />
paket add ContainerConfigurationMonitor --version 0.1.2
#r "nuget: ContainerConfigurationMonitor, 0.1.2"
// 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
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.
Add the package to your project :
dotnet add package ContainerConfigurationMonitorService
Or add the package with Package Manager
Install-Package ContainerConfigurationMonitorService
Register the
ContainerConfigurationMonitorService
in yourStartup.cs
orProgram.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.jso
n 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 | Versions 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. |
-
.NETStandard 2.0
- ContainerFileSystemWatcher (>= 0.1.2)
- Microsoft.Extensions.Hosting (>= 9.0.0-preview.4.24266.19)
- Microsoft.Extensions.Options (>= 9.0.0-preview.4.24266.19)
- Moq (>= 4.20.70)
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 | 55 | 7/28/2024 |