我正在使用Discord.Net和Discord.Addons.Hosting软件包开发一个不和谐的机器人。由于某种原因,当我调用IHost.StopAsync()方法时,我的IHostedServices中的所有StopAsync方法都会被调用两次。
下面是其中一个StopAsync方法:
public override Task StopAsync(CancellationToken stoppingToken)
{
Logger.LogInformation("Client [{clientId}] is stopped.", Client.CurrentUser.Id);
return base.StopAsync(stoppingToken);
}我使用命令关闭我的机器人!关闭:
[Command("shutdown")]
[RequireUserPermission(GuildPermission.Administrator, Group = "Permission")]
[RequireOwner(Group = "Permission")]
public async Task Stop()
{
await _host.StopAsync();
}以下是我试图关闭机器人时的日志:
[11:08:41 INF] Application is shutting down...
[11:08:46 INF] Client [1007213990742610001] is stopped.
[11:08:46 INF] Client [1007213990742610001] is stopped.
[11:08:46 INF] Discord.NET hosted service is stopping
[11:08:46 INF] Discord.NET hosted service is stopping
[11:08:46 INF] Gateway: Disconnecting发布于 2022-08-24 15:05:48
RunAsync itself calls StopAsync。如果要从主机内部停止主机,请调用IHostApplicationLifetime.StopApplication()而不是IHost.StopAsync()。
https://stackoverflow.com/questions/73469803
复制相似问题