向Bot API发送消息时,连接意外关闭错误

时间:2019-05-05 11:12:39

标签: c# telegram telegram-bot

我们有一个使用电报bot API的c#应用程序,它使用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

有人可以帮助我解决此问题吗? 预先感谢。

1 个答案:

答案 0 :(得分:1)

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