Last active
October 18, 2025 04:30
-
-
Save ravindUwU/8ce19a39103658c4afd5914da75b9a28 to your computer and use it in GitHub Desktop.
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
| class SomeOptionsHostedService(IOptionsMonitor<SomeOptions> monitor): IHostedService, IDisposable | |
| { | |
| private CancellationTokenSource? stopping; | |
| private IDisposable? listener; | |
| public async Task StartAsync(CancellationToken cancellationToken) | |
| { | |
| stopping = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); | |
| listener = monitor.OnChange(async (value) => await UseAsync(value, stopping.Token)); | |
| await UseAsync(monitor.CurrentValue, cancellationToken); | |
| } | |
| public Task StopAsync(CancellationToken cancellationToken) | |
| { | |
| stopping?.Cancel(); | |
| return Task.CompletedTask; | |
| } | |
| public void Dispose() | |
| { | |
| stopping?.Dispose(); | |
| listener?.Dispose(); | |
| } | |
| private async Task UseAsync(SomeOptions options, CancellationToken cancellationToken = default) | |
| { | |
| await Task.CompletedTask; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment