gpt4 book ai didi

c# - WinHttp异常 : The connection with the server was terminated abnormally

转载 作者:可可西里 更新时间:2023-11-01 15:29:28 25 4
gpt4 key购买 nike

我有一个 ASP.Net Core Wep API 项目,它执行以下任务:

  • 通过名为 ProcessController 的 Controller 接收请求。
  • 接收传入请求并将数据格式化为字符串。
  • 使用以下 HttpProcessor 类中的 PostChargeAsync 方法将上述格式化消息发布到 URL 并等待响应消息以执行进一步处理。

请注意,HttpClient 是使用 IoC 注入(inject)的,并且是单例。

public class HttpProcessor
{
private readonly HttpClient _httpClient;

public HttpProcessor(HttpClient httpClient)
{
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
}

public async Task<string> PostChargeAsync(string payload)
{
using (var httpRequest = BuildHttpRequest(payload))
{
try
{
using (var httpResponse = await _httpClient.SendAsync(httpRequest).ConfigureAwait(false))
{
httpResponse.EnsureSuccessStatusCode();

using (var httpContent = httpResponse.Content)
{
return await httpContent.ReadAsStringAsync();
}
}
}
catch (HttpRequestException ex)
{
throw;
}
catch (TimeoutException ex)
{
throw;
}
catch (System.Exception ex)
{
throw;
}
}
}

private HttpRequestMessage BuildHttpRequest(string content)
{
return new HttpRequestMessage
{
Content = new StringContent(content, Encoding.UTF8, "application/xml"),
Method = HttpMethod.Post,
RequestUri = new Uri("https://test/process"),
};
}
}

问题 是当在 ProcessController 上发送并行请求(~30 - 50 个请求/分钟)并调用 PostChargeAsync 方法时,我得到以下异常:

System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.Http.WinHttpException: The connection with the server was terminated abnormally

我尝试在 Web.Config 中添加以下设置以增加 connectionManagement 中的最大连接数:

  <system.net>
<connectionManagement>
<add address="*" maxconnection="100"/>
</connectionManagement>
</system.net>

但我仍然遇到上述异常。

有什么想法会导致上述问题吗?

最佳答案

我们从 HttpClient 迁移到 HttpWebRequest,上述问题已解决。 .Net Core Runtime 2.0 上的 HttpClient 存在一些问题。请注意,在创建这篇文章时,我使用的是 .NET Core SDK 2.1.4 和 .NET Core Runtime 2.0.5。

关于c# - WinHttp异常 : The connection with the server was terminated abnormally,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48395459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com