简体   繁体   English

dotnet 核心 2.1 httpclient System.Net.Sockets.SocketException

[英]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.

相关问题 HttpClient、Azure 和 System.Net.Sockets.SocketException:怎么办? - HttpClient, Azure and System.Net.Sockets.SocketException: what to do? System.Net.Sockets.SocketException - System.Net.Sockets.SocketException postgres 连接上的 System.Net.Sockets.SocketException - System.Net.Sockets.SocketException on postgres connection System.Net.Sockets.SocketException 灾难 - System.Net.Sockets.SocketException Disaster C# HttpClient多个Get请求同时给出System.Net.Sockets.SocketException(10060)异常 - C# HttpClient Multiple Get Requests at the same time giving System.Net.Sockets.SocketException (10060) Exception ASP.NET CORE 2 System.Net.Sockets.SocketException:每当上传文件时 - ASP.NET CORE 2 System.Net.Sockets.SocketException: Whenever uploading files .NET服务System.Net.Sockets.SocketException:'未知此类主机' - .NET Service System.Net.Sockets.SocketException: 'No such host is known' System.Net.Sockets.SocketException (0x80004005): 没有这样的主机是已知的 - System.Net.Sockets.SocketException (0x80004005): No such host is known 2008R2服务器上的System.Net.Sockets.SocketException - System.Net.Sockets.SocketException on 2008R2 Server EventHub 发送消息,但 Application Insights 上的 System.Net.Sockets.SocketException - EventHub send message, but System.Net.Sockets.SocketException on Application Insights
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM