C# 向bot api发送消息时出错,连接意外关闭

C# 向bot api发送消息时出错,连接意外关闭,c#,telegram,telegram-bot,C#,Telegram,Telegram Bot,我们有一个c#应用程序,它使用电报机器人API,使用SendTextMessageAsync向许多订户发送大量消息 await _client.SendTextMessageAsync( telegramChatId, message, parseMode: ParseMode.Html, repl

我们有一个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.

异常消息没有给出任何有关原因的线索。 有没有办法让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
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Net.Http.HttpClient.d_u58.MoveNext()上
---来自引发异常的上一个位置的堆栈结束跟踪---
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在telegate.Bot.TelegramBotClient.d_u54`1.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()中
在MelkRadar.Core.Service.Telegram.Common.TelegramBot`1.c_uu显示c:\Repos\MelkRadar\src\MelkRadar\Core\MelkRadar.Core.Service\Telegram\Common\TelegramBot.cs中的Class19_0.d.MoveNext():第113行
---来自引发异常的上一个位置的堆栈结束跟踪---
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在c:\projects\Polly\src\Polly.Shared\Policy.Async.cs中的Polly.Policy.c\u显示Class181_0`1.d.MoveNext():第0行
---来自引发异常的上一个位置的堆栈结束跟踪---
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在c:\projects\Polly\src\Polly.Shared\Retry\RetrySyntaxAsync.c\uu中显示Class25\u 1.d.MoveNext(),第545行
---来自引发异常的上一个位置的堆栈结束跟踪---
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在C:\projects\Polly\src\Polly.Shared\Retry\RetryEngineAsync.cs中的Polly.Retry.RetryEngine.d_u1`1.MoveNext()处:第29行
有人能帮我解决这个问题吗?
提前感谢。

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

当客户端断开连接时,服务器是否正在关闭连接?服务器中的侦听器最多只允许400个连接。因此,当连接超过400个时,服务器不允许更多连接。因此,在允许更多连接之前,您必须关闭服务器上的连接。@jdweng但为什么以前没有发生这种情况?这可能是您第一次收到400个请求。不,我确定。。。
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