当我 运行 我的电报作为后台服务 (IHostedService) 接收时,电报机器人 Console.Readline 阻止整个主机
Telegram Bot Console.Readline Block Whole My Host when I Run my Telegram Receive as Background Service (IHostedService)
我的项目有 API 个端点和后台 TelegramBot 通信后台服务,您可以在下面看到我的后台服务:
public class BackgroundWorker: BackgroundService
{
private readonly IOptions<TelegramSettings> _telegramSettings;
private readonly IOptions<SqliteConnectionString> _sqlite;
public BackgroundWorker(IOptions<TelegramSettings>
telegramSettings, IOptions<SqliteConnectionString> sqlite)
{
_telegramSettings = telegramSettings;
_sqlite = sqlite;
}
public override async Task StartAsync(CancellationToken
cancellationToken)
{
await base.StartAsync(cancellationToken);
}
protected override Task ExecuteAsync(CancellationToken
stoppingToken)
{
var telegramSocial = new
TelegramSocial(_telegramSettings,_sqlite);
telegramSocial.ReceiveMessage();
return Task.CompletedTask;
}
}
还有我的 Telegram Bot 接收 如下所示:
public void ReceiveMessage()
{
using var cts = new CancellationTokenSource();
// StartReceiving does not block the caller
//thread.Receiving is done on the ThreadPool.
var receiverOptions = new ReceiverOptions
{
AllowedUpdates = { } // receive all update types
};
_botClient.StartReceiving(HandleUpdateAsync,
HandleErrorAsync, receiverOptions,
cancellationToken: cts.Token);
Console.ReadLine();
// Send cancellation request to stop bot
cts.Cancel();
}
enter code here
如您所见,Console.ReadLine() 阻止了我的整个主机和我的 APIEndPoints 不起作用
现在我可以做些什么来解决这个问题或用 Console.ReadLine().
替换的东西
Telegram.Bot.Extensions.Polling 1.0.0-alpha.1
.Net Core 5
抱歉英语不好。
更改 StartReceive 方法并仅使用 ReceiveAsync 及其工作并且不要阻止我的主机代码 Att 下面:
public async Task ReceiveMessage()
{
using var cts = new CancellationTokenSource();
// StartReceiving does not block the caller thread. Receiving is
//done on the ThreadPool.
var receiverOptions = new ReceiverOptions
{
AllowedUpdates = { } // receive all update types
};
await _botClient.ReceiveAsync(HandleUpdateAsync, HandleErrorAsync,
receiverOptions,
cancellationToken: cts.Token);
Console.ReadLine();
// Send cancellation request to stop bot
cts.Cancel();
}
我的项目有 API 个端点和后台 TelegramBot 通信后台服务,您可以在下面看到我的后台服务:
public class BackgroundWorker: BackgroundService
{
private readonly IOptions<TelegramSettings> _telegramSettings;
private readonly IOptions<SqliteConnectionString> _sqlite;
public BackgroundWorker(IOptions<TelegramSettings>
telegramSettings, IOptions<SqliteConnectionString> sqlite)
{
_telegramSettings = telegramSettings;
_sqlite = sqlite;
}
public override async Task StartAsync(CancellationToken
cancellationToken)
{
await base.StartAsync(cancellationToken);
}
protected override Task ExecuteAsync(CancellationToken
stoppingToken)
{
var telegramSocial = new
TelegramSocial(_telegramSettings,_sqlite);
telegramSocial.ReceiveMessage();
return Task.CompletedTask;
}
}
还有我的 Telegram Bot 接收 如下所示:
public void ReceiveMessage()
{
using var cts = new CancellationTokenSource();
// StartReceiving does not block the caller
//thread.Receiving is done on the ThreadPool.
var receiverOptions = new ReceiverOptions
{
AllowedUpdates = { } // receive all update types
};
_botClient.StartReceiving(HandleUpdateAsync,
HandleErrorAsync, receiverOptions,
cancellationToken: cts.Token);
Console.ReadLine();
// Send cancellation request to stop bot
cts.Cancel();
}
enter code here
如您所见,Console.ReadLine() 阻止了我的整个主机和我的 APIEndPoints 不起作用
现在我可以做些什么来解决这个问题或用 Console.ReadLine().
替换的东西Telegram.Bot.Extensions.Polling 1.0.0-alpha.1
.Net Core 5
抱歉英语不好。
更改 StartReceive 方法并仅使用 ReceiveAsync 及其工作并且不要阻止我的主机代码 Att 下面:
public async Task ReceiveMessage()
{
using var cts = new CancellationTokenSource();
// StartReceiving does not block the caller thread. Receiving is
//done on the ThreadPool.
var receiverOptions = new ReceiverOptions
{
AllowedUpdates = { } // receive all update types
};
await _botClient.ReceiveAsync(HandleUpdateAsync, HandleErrorAsync,
receiverOptions,
cancellationToken: cts.Token);
Console.ReadLine();
// Send cancellation request to stop bot
cts.Cancel();
}