c# - 将消息发送到 bot api 时连接意外关闭错误

标签 c# telegram telegram-bot

我们的 C# 应用程序使用 Telegram Bot API 向使用 SendTextMessageAsync 的大量订阅者发送大量消息。

    await _client.SendTextMessageAsync(
                        telegramChatId,
                        message,
                        parseMode: ParseMode.Html,
                        replyMarkup: keyboard,
                        disableWebPagePreview: disableWebPagePreview,
                        disableNotification: disableNotification
                    );

到目前为止一切正常。但最近我们开始为某些请求获取此异常:

The underlying connection was closed: The connection was closed unexpectedly.

The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.

异常消息没有提供任何关于原因的线索。 有没有办法让visual studio显示更多细节!? 该程序会一直运行,直到发送了大约 400 个第一个请求。 由于请求数超过 400,它在尝试发出请求时引发了该异常。 此外,请求之间有一秒的延迟,因此我们不会得到 TooManyRequest 异常。 无论我们重试多少次发送请求。我会一次又一次地遇到同样的错误。

这是异常的堆栈跟踪:

at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)

这是一个异常堆栈跟踪:

at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Net.Http.HttpClient.<FinishSendAsyncBuffered>d__58.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Telegram.Bot.TelegramBotClient.<MakeRequestAsync>d__54`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at MelkRadar.Core.Service.Telegram.Common.TelegramBot`1.<>c__DisplayClass19_0.<<SendTextMessageAsync>b__0>d.MoveNext() in C:\Repos\MelkRadar\src\MelkRadar\Core\MelkRadar.Core.Service\Telegram\Common\TelegramBot.cs:line 113
 --- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Polly.Policy.<>c__DisplayClass181_0`1.<<ExecuteAsyncInternal>b__0>d.MoveNext() in C:\projects\polly\src\Polly.Shared\Policy.Async.cs:line 0
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Polly.RetrySyntaxAsync.<>c__DisplayClass25_1.<<WaitAndRetryAsync>b__1>d.MoveNext() in C:\projects\polly\src\Polly.Shared\Retry\RetrySyntaxAsync.cs:line 545
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Polly.Retry.RetryEngine.<ImplementationAsync>d__1`1.MoveNext() in C:\projects\polly\src\Polly.Shared\Retry\RetryEngineAsync.cs:line 29

谁能帮我解决这个问题? 提前致谢。

最佳答案

当我停止我的 VPN 时,问题就解决了! 我不知道它如何真正影响我的请求以及 Telegram 服务器和我的应用程序之间的连接。但问题似乎是由 VPN 引起的。

关于c# - 将消息发送到 bot api 时连接意外关闭错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55991341/

相关文章:

telegram-bot - 向所有机器人用户发送消息(电报 API)

c# - 连接c#.net到phpmyadmin数据库(MysqL)

c# - 具有服务层、业务层和 Entity Framework 的 N 层架构

c# - 屏幕阅读器 WPF Groupstyles

c# - 你必须使用命令来遵循 MVVM 模式吗?

python - 在 telethon telegram bot 中发送民意调查

encryption - 如何解密 Telegram 网络请求

telegram-bot - 使用 telegram 和 webhook 发送多条消息

php - 如何区分 'Message' 更新和 'Callback Query' 更新? ( Telegram Bot API)

c# - Telegram Bot Api 库 (.Net) 中标题的换行符