我正在使用Discord.Net和Discord.Addons.Hosting软件包开发Discord机器人。出于某种原因,当我调用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
RunAsync
本身调用StopAsync
。如果要从主机中停止主机,请调用IHostApplicationLifetime.StopApplication()
而不是IHost.StopAsync()
。