[英]dotnet core 2.1 httpclient System.Net.Sockets.SocketException
since 2 weeks by trying connect our backendsystem some times we get the following exception:自 2 周以来,有时通过尝试连接我们的后端系统,我们会收到以下异常:
System.Net.Http.HttpRequestException: Unknown error -1 ---> System.Net.Sockets.SocketException: Unknown error -1 at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken) --- End of inner exception stack trace --- at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken) at System.Threading.Tasks.ValueTask`1.get_Result() 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()
But at this time, I can call the API Server via Postman.但是这个时候,我可以通过 Postman 调用 API Server。 My Code calling the API Server is Like:我调用 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;
}
}
Where ClientFactory()
function looks like其中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;
}
I have no idea how I could debugg this problem.我不知道如何调试这个问题。 The application is on a Cloud Foundry server running on kestrel web server.该应用程序位于在 kestrel Web 服务器上运行的 Cloud Foundry 服务器上。
UPDATE更新
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;
}
Where ClientFactory() function now looks looks like 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;
}
Now I use the IHttpClientFactory injected by the constructor:现在我使用构造函数注入的 IHttpClientFactory :
public MyConstructor(IHttpClientFactory clientFactory)
{
this.clientFactory = clientFactory;
}
But it dosen't help I still get the Exception above and other exception like:但这并没有帮助我仍然得到上面的异常和其他异常,例如:
System.Net.Http.HttpRequestException: Too many open files in system --- System.Net.Sockets.SocketException: Too many open files in system at System.Net.Sockets.Socket..ctor(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType) at System.Net.Sockets.DualSocketMultipleConnectAsync..ctor(SocketType socketType, ProtocolType protocolType) at System.Net.Sockets.Socket.ConnectAsync(SocketType socketType, ProtocolType protocolType, SocketAsyncEventArgs e) at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken) 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 取消令牌)
So my question again, how can I debug the problem?所以我的问题又来了,我该如何调试问题? It occurrences only on the prodcution and after few hours or days...它只发生在生产和几个小时或几天后......
You should NOT create a new instance of HttpClient all the time.你不应该创建一个HttpClient一个新的实例的所有时间。 You should reuse it.你应该重复使用它。 If you dont reuse it you can run into socket exhaustion (and a bunch of other problems).如果您不重用它,您可能会遇到套接字耗尽(以及一系列其他问题)。
https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/ https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/
Since you are using dotnet core, have a look at HttpClientFactory https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests由于您使用的是 dotnet 核心,请查看 HttpClientFactory https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-要求
So a quick fix for you would be to create a new static HttpClient and not dispose it.因此,您的快速修复方法是创建一个新的静态 HttpClient 而不是处置它。 But then you can get DNS issues, so don't do that .但是你可能会遇到 DNS 问题,所以不要这样做。 https://github.com/dotnet/corefx/issues/11224 https://github.com/dotnet/corefx/issues/11224
So: Use HttpClientFactory, Microsoft has solved all the problems mentioned above :)所以:使用HttpClientFactory,微软已经解决了上面提到的所有问题:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.