我在严重HttpClient负载下遇到严重问题.我创建了一个GitHub repo来演示这个问题.非常感谢任何帮助.是的,我HttpClient每个权限只使用一个.http和https也有同样的问题.这不是服务器端问题,因为服务器始终保持响应.
https://github.com/erikbra/HttpClientProblems/issues/1
通常会收到以下错误.System.Net跟踪日志说它正在调用::Abort连接.但很难获得更多信息.
System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.Http.WinHttpException: The connection with the server was terminated abnormally
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Threading.Tasks.RendezvousAwaitable`1.GetResult()
at System.Net.Http.WinHttpHandler.<StartRequest>d__105.MoveNext()
--- End of inner exception stack trace ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
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 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at UnitTests.Call_Api.<CallApi>d__22.MoveNext() in C:\Users\ErikBrandstadmoen\Source\Repos\HttpClientProblems\UnitTests.NetCore\Call_Api.cs:line 204
--- 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 UnitTests.Call_Api.<Measure>d__23.MoveNext() in C:\Users\ErikBrandstadmoen\Source\Repos\HttpClientProblems\UnitTests.NetCore\Call_Api.cs:line 219
Run Code Online (Sandbox Code Playgroud)
[UPDATE]
确切地说,我想了解发生了什么.显然连接正在被削减,但为什么呢?服务器仍在响应,因此它不是服务器端的限制.瓶颈在哪里?
如果我设置ServicePointManager.DefaultConnectionLimit为2,并启动4个任务,会发生什么?它们不会轮流送达吗?与128和1000相似,但在某处它会停止缩放.我怎样才能进一步调查瓶颈是什么?我只得到一个TaskCanceledException,而且没有更多细节.有小费吗?
我没有详细介绍您的 github 解决方案的代码详细信息,但这听起来像是预期的行为。每个最大连接数有一个设置,ServicePoint当您进行并行异步执行时,在运行时间较长的请求上,您通常会更快地耗尽连接。
这可能是一个很好的起点: https ://learn.microsoft.com/en-us/dotnet/framework/network-programming/managing-connections
关于多少个连接是可以的,没有明确、统一的答案,但您应该将连接数保持MaxDegreeOfParalelism在允许的最大连接数以下,这样就安全了。
[更新]
另外,您可能想查看的是System.Net.Http.WinHttpHandler:
https://msdn.microsoft.com/en-us/library/system.net.http.winhttphandler(v=vs.105).aspx
当您创建 的实例时HttpClient,您可以执行以下操作:
var httpMessageHandler = new System.Net.Http.WinHttpHandler();
//here you set different handler options (see docs link above)
var httpClient = new HttpClient(httpMessageHandler);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
727 次 |
| 最近记录: |