///------------------------------------------------------------------------------------------------- /// <summary> /// This method is called when the /// <see cref="T:Microsoft.Extensions.Hosting.IHostedService" /> starts. Starts the daemon /// service asynchronous, waits for cancellation and stops the daemon service asynchronous. /// </summary> /// /// <param name="stoppingToken"> Triggered when /// <see cref="M:Microsoft.Extensions.Hosting.IHostedService.StopAsync(System.Threading.CancellationToken)" /> /// is called. </param> /// /// <returns> /// A <see cref="T:System.Threading.Tasks.Task" /> that represents the long running /// operations. /// </returns> /// /// <seealso cref="Microsoft.Extensions.Hosting.BackgroundService.ExecuteAsync(CancellationToken)"/> ///------------------------------------------------------------------------------------------------- protected override async Task ExecuteAsync(CancellationToken stoppingToken) { await _daemonService.StartAsync(); while (!stoppingToken.IsCancellationRequested) { await Task.Delay(1000, stoppingToken); } await _daemonService.StopAsync(); }
public virtual Task StartAsync(CancellationToken cancellationToken) { logger.LogInformation($"Starting daemon: {daemonName}"); // Register lifetime events applicationLifetime.ApplicationStarted.Register(OnStarted); applicationLifetime.ApplicationStopping.Register(OnStopping); applicationLifetime.ApplicationStopped.Register(OnStopped); return(daemonService.StartAsync(cancellationToken)); }