![](/img/trans.png)
[英]HttpClient, Azure and System.Net.Sockets.SocketException: what to do?
[英]dotnet core 2.1 httpclient System.Net.Sockets.SocketException
自 2 周以来,有时通过尝试连接我们的后端系统,我们会收到以下异常:
System.Net.Http.HttpRequestException: 未知错误 -1 ---> System.Net.Sockets.SocketException: System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancelationToken) 出现未知错误 -1 -- - 内部异常堆栈跟踪结束 --- 在 System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancelationToken) 在 System.Threading.Tasks.ValueTask`1.get_Result()
但是这个时候,我可以通过 Postman 调用 API Server。 我调用 API 服务器的代码如下:
using (var client = this.ClientFactory(serviceUserName, serviceUserPassword))
{
try
{
using (var response = await client.SendAsync(request, System.Net.Http.HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false))
{
return checkAction(response);
}
}
catch (HttpRequestException ex)
{
this.logger.LogError(ex, "http error occured while checking availability for " + url);
return false;
}
catch (TaskCanceledException ex)
{
this.logger.LogError(ex, url + " timed out");
return false;
}
catch (Exception ex)
{
this.logger.LogError(ex, url + " general exception!");
return false;
}
}
其中ClientFactory()
函数看起来像
private HttpClient ClientFactory(string serviceUserName, string serviceUserPassword)
{
var client = new HttpClient();
client.Timeout = TimeSpan.FromSeconds(this.clientTimeoutSeconds);
if (!string.IsNullOrEmpty(serviceUserName) && !string.IsNullOrEmpty(serviceUserPassword))
{
var basicAuthToken = Convert.ToBase64String(Encoding.ASCII.GetBytes(serviceUserName + ":" + serviceUserPassword));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", basicAuthToken);
}
return client;
}
我不知道如何调试这个问题。 该应用程序位于在 kestrel Web 服务器上运行的 Cloud Foundry 服务器上。
更新
var client = this.ClientFactory(serviceUserName, serviceUserPassword);
try
{
var response = await client.SendAsync(request, System.Net.Http.HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
return checkAction(response);
}
catch (HttpRequestException ex)
{
this.logger.LogError(ex, "http error occured while checking availability for " + url);
return false;
}
catch (TaskCanceledException ex)
{
this.logger.LogError(ex, "availability check for " + url + " timed out");
return false;
}
catch (Exception ex)
{
this.logger.LogError(ex, "availability check for " + url + " general exception!");
return false;
}
ClientFactory() 函数现在看起来像
private HttpClient ClientFactory(string serviceUserName, string serviceUserPassword)
{
var client = this.clientFactory.CreateClient();
client.Timeout = TimeSpan.FromSeconds(this.clientTimeoutSeconds);
if (!string.IsNullOrEmpty(serviceUserName) && !string.IsNullOrEmpty(serviceUserPassword))
{
var basicAuthToken = Convert.ToBase64String(Encoding.ASCII.GetBytes(serviceUserName + ":" + serviceUserPassword));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", basicAuthToken);
}
return client;
}
现在我使用构造函数注入的 IHttpClientFactory :
public MyConstructor(IHttpClientFactory clientFactory)
{
this.clientFactory = clientFactory;
}
但这并没有帮助我仍然得到上面的异常和其他异常,例如:
System.Net.Http.HttpRequestException: 系统中打开的文件太多 --- System.Net.Sockets.SocketException: System.Net.Sockets.Socket..ctor(AddressFamily addressFamily, SocketType socketType, ProtocolType) 系统中打开的文件太多protocolType) 在 System.Net.Sockets.DualSocketMultipleConnectAsync..ctor(SocketType socketType, ProtocolType protocolType) 在 System.Net.Sockets.Socket.ConnectAsync(SocketType socketType, ProtocolType protocolType, SocketAsyncEventArgs e) 在 System.Net.Http.ConnectHelper.ConnectAsync (字符串主机,Int32 端口,CancellationToken 取消令牌)
所以我的问题又来了,我该如何调试问题? 它只发生在生产和几个小时或几天后......
你不应该创建一个HttpClient一个新的实例的所有时间。 你应该重复使用它。 如果您不重用它,您可能会遇到套接字耗尽(以及一系列其他问题)。
https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/
由于您使用的是 dotnet 核心,请查看 HttpClientFactory https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-要求
因此,您的快速修复方法是创建一个新的静态 HttpClient 而不是处置它。 但是你可能会遇到 DNS 问题,所以不要这样做。 https://github.com/dotnet/corefx/issues/11224
所以:使用HttpClientFactory,微软已经解决了上面提到的所有问题:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.