title: dotnet core 2.1 httpclient System.Net.Sockets.SocketException
body: <p>since 2 weeks by trying connect our backendsystem some times we get the following exception:</p>
  
  <blockquote>
    <p>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()</p>
  </blockquote>
  
  <p>But at this time, I can call the API Server via Postman.
  My Code calling the API Server is Like:</p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p>Where <code>ClientFactory()</code> function looks like</p>
  
  <pre><code>private HttpClient ClientFactory(string serviceUserName, string serviceUserPassword)
  {
      var client = new HttpClient();
      client.Timeout = TimeSpan.FromSeconds(this.clientTimeoutSeconds);
      if (!string.IsNullOrEmpty(serviceUserName) &amp;&amp; !string.IsNullOrEmpty(serviceUserPassword))
      {
          var basicAuthToken = Convert.ToBase64String(Encoding.ASCII.GetBytes(serviceUserName + ":" + serviceUserPassword));
          client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", basicAuthToken);
      }
  
      return client;
  }
  </code></pre>
  
  <p>I have no idea how I could debugg this problem. 
  The application is on a Cloud Foundry server running on kestrel web server.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <pre><code>            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;
          }
  </code></pre>
  
  <p>Where ClientFactory() function now looks looks like</p>
  
  <pre><code>        private HttpClient ClientFactory(string serviceUserName, string serviceUserPassword)
      {
          var client = this.clientFactory.CreateClient();
          client.Timeout = TimeSpan.FromSeconds(this.clientTimeoutSeconds);
          if (!string.IsNullOrEmpty(serviceUserName) &amp;&amp; !string.IsNullOrEmpty(serviceUserPassword))
          {
              var basicAuthToken = Convert.ToBase64String(Encoding.ASCII.GetBytes(serviceUserName + ":" + serviceUserPassword));
              client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", basicAuthToken);
          }
  
          return client;
      }
  </code></pre>
  
  <p>Now I use the IHttpClientFactory injected by the constructor:</p>
  
  <pre><code>        public MyConstructor(IHttpClientFactory clientFactory)
      {
          this.clientFactory = clientFactory;
      }
  </code></pre>
  
  <p>But it dosen't help
  I still get the Exception above and other exception like:</p>
  
  <blockquote>
    <p>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)</p>
  </blockquote>
  
  <p>So my question again, how can I debug the problem? It occurrences only on the prodcution and after few hours or days...</p>