【问题标题】:The connection was closed unexpectedly error in sending message to bot api向 bot api 发送消息时连接意外关闭错误
【发布时间】:2019-09-23 06:30:47
【问题描述】:

我们有 c# 应用程序,它使用电报机器人 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.

异常消息没有提供任何有关原因的线索。 有没有办法让视觉工作室显示更多细节!? 该程序会一直运行,直到发送大约 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

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

【问题讨论】:

  • 客户端断开连接时服务器是否关闭连接?服务器中的监听器只允许最多 400 个连接。因此,当建立超过 400 个连接时,服务器不允许更多连接。因此,您必须在允许更多连接之前关闭服务器上的连接。
  • @jdweng 但是为什么以前没有发生呢?
  • 可能这是您第一次收到 400 个请求。
  • 不,我确定...

标签: c# telegram telegram-bot


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-02
    • 2015-11-22
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多