Window service API encounter 408 request timeout

94 Views Asked by At

I had a window service using .net 4.6 framework that calls keycloak API when timeelapsed.

The first call after start and first call after idle for a period would always ended up with 408 request timeout.

This problem only happen in window server 2019 and not on my local laptop.

I tried to "useproxy=false", "set request host address by ipv4".

The timeout took about 15 seconds to happen.

using(client = getHttpCient(jwt).Result) {
  var uri = new Uri("https://somedomain.com/auth/realms/master/protocol/openid-connect/token");
  var builder = new UriBuilder(uri);
  var addresses = await Dns.GetHostAddressesAsync(uri.Host);
  builder.Host = addresses
    .First(x => x.AddressFamily == AddressFamily.InterNetwork) //IPV4 only
    .ToString();
  var request = new HttpRequestMessage(method, builder.Uri);
  request.Headers.Host = uri.Host;
  request.Content = content;
  var responseMessage = await client.SendAsync(request);

}

Public httpclient getHttpCient(bool jwt) {
  ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

  HttpClientHandler httpClientHandler = new HttpClientHandler() {
    Proxy = null,
      UseProxy = false,
      UseDefaultCredentials = true,
      ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => {
        return true;
      }
  };

  client = new HttpClient(httpClientHandler, disposeHandler: false) {
    BaseAddress = new Uri(AppConfig.gatewayHost)
  };

  return client;
}
0

There are 0 best solutions below