Skip to content

Commit 49d5ac4

Browse files
committed
Simplify HostApplicationLifetime handling in BotApp.cs
Reordered retrieval of HostApplicationLifetime service to occur after starting hosted services. Removed individual and global ApplicationStopping event registrations. Called NotifyStarted on hostApplicationLifetime post hosted services startup. These changes streamline the startup process and reduce redundant event handling.
1 parent ceda38a commit 49d5ac4

1 file changed

Lines changed: 1 addition & 11 deletions

File tree

Sources/TelegramBot/BotApp.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,22 +118,12 @@ public async Task StartAsync(CancellationToken cancellationToken = default)
118118
throw ex;
119119
}
120120
var hostedServices = _serviceProvider.GetServices<IHostedService>();
121-
var hostApplicationLifetime = _serviceProvider.GetRequiredService<HostApplicationLifetime>();
122121
foreach (var hostedService in hostedServices)
123122
{
124123
await hostedService.StartAsync(mergedToken);
125124
_logger.LogInformation("Started '{hostedService}'.", hostedService.GetType().Name);
126-
hostApplicationLifetime.ApplicationStopping.Register(() =>
127-
{
128-
_logger.LogInformation("Stopping '{hostedService}'...", hostedService.GetType().Name);
129-
hostedService.StopAsync(mergedToken).Wait();
130-
});
131125
}
132-
hostApplicationLifetime.ApplicationStopping.Register(() =>
133-
{
134-
_logger.LogInformation("Application stopping event received.");
135-
_cancellationTokenSource.Cancel();
136-
});
126+
var hostApplicationLifetime = _serviceProvider.GetRequiredService<HostApplicationLifetime>();
137127
hostApplicationLifetime.NotifyStarted();
138128
}
139129

0 commit comments

Comments
 (0)