Last active
March 29, 2022 16:41
-
-
Save itn3000/5b2464404a6a9fda62dc5839f11e8df7 to your computer and use it in GitHub Desktop.
sandbox for getting notification of IConfiguration changes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // dotnet add package Microsoft.Extensions.Hosting; | |
| // dotnet add package System.Reactive; | |
| using Microsoft.Extensions.Options; | |
| using Microsoft.Extensions.Configuration; | |
| using System.Reactive.Concurrency; | |
| using System.Reactive.Subjects; | |
| using System.Reactive.Linq; | |
| namespace hostingtest; | |
| public class Worker : BackgroundService | |
| { | |
| private readonly ILogger<Worker> _logger; | |
| IConfiguration _Configuration; | |
| ManualResetEventSlim _Changed = new ManualResetEventSlim(); | |
| Subject<int> _Subscription; | |
| IDisposable? _CurrentSubscription = null; | |
| public Worker(ILogger<Worker> logger, IConfiguration cfg) | |
| { | |
| _logger = logger; | |
| _Configuration = cfg; | |
| _Subscription = new Subject<int>(); | |
| _CurrentSubscription = cfg.GetReloadToken().RegisterChangeCallback(ProcessLogger, _Subscription); | |
| } | |
| void ProcessLogger(object state) | |
| { | |
| var sub = (Subject<int>)state; | |
| var currentSubscription = _CurrentSubscription; | |
| // var section = _Configuration.GetSection("Option1"); | |
| // _logger.LogInformation("Option1 = {0}", section["Value1"]); | |
| // _Changed.Set(); | |
| // _Configuration.GetReloadToken().RegisterChangeCallback(ProcessLogger, state); | |
| currentSubscription?.Dispose(); | |
| sub.OnNext(0); | |
| _logger.LogInformation("proclogger end"); | |
| } | |
| protected override async Task ExecuteAsync(CancellationToken stoppingToken) | |
| { | |
| using var timer = new PeriodicTimer(TimeSpan.FromSeconds(1)); | |
| using var subscription = _Subscription.SubscribeOn(TaskPoolScheduler.Default) | |
| // .Throttle(TimeSpan.FromSeconds(1)) | |
| .Subscribe(_ => | |
| { | |
| var section = _Configuration.GetSection("Option1"); | |
| _logger.LogInformation("{0} Option1 = {1}", DateTime.Now, section["Value1"]); | |
| _CurrentSubscription = _Configuration.GetReloadToken().RegisterChangeCallback(ProcessLogger, _Subscription); | |
| }); | |
| while (await timer.WaitForNextTickAsync(stoppingToken)) | |
| { | |
| // if (_Changed.IsSet) | |
| // { | |
| // _Changed.Reset(); | |
| // var section = _Configuration.GetSection("Option1"); | |
| // _logger.LogInformation("Option1 = {0}", section["Value1"]); | |
| // _Configuration.GetReloadToken().RegisterChangeCallback(ProcessLogger, null); | |
| // _logger.LogInformation("piyo"); | |
| // } | |
| // await Task.Delay(1000, stoppingToken); | |
| } | |
| } | |
| public override void Dispose() | |
| { | |
| _Changed.Dispose(); | |
| _Subscription.Dispose(); | |
| _CurrentSubscription?.Dispose(); | |
| base.Dispose(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment