skip to Main Content

A straightforward question, yet one that currently eludes me an answer.
I’m attempting to make a request to my API from a basic console application, and the request takes more than 30 seconds to complete. Interestingly, when I execute the application in a Windows environment, everything works fine. However, when I run it inside a container using Docker Desktop, the POST request consistently stops after 30 seconds. I suspect there may be a timeout configuration issue within my Docker setup. This behavior persists even when deploying the application in OpenShift. Any insights or suggestions would be greatly appreciated.

Here is the code:

var client = new HttpClient();

var request = new HttpRequestMessage(HttpMethod.Post, {URL});

var content = new StringContent({requestBody}, null, "application/json");
request.Content = content;

var response = await client.SendAsync(request); // it crashes here 

HttpRequestException:

{System.Net.Http.HttpRequestException: An error occurred while sending the request.
System.IO.IOException: The response ended prematurely.
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
End of inner exception stack trace ---
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.DecompressionHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
at RestSharp.RestClient.ExecuteRequestAsync(RestRequest request, CancellationToken cancellationToken)}

I tried to change some docker settings to increase the timeout and be sure that it’s not a proxy issue 🙂

2

Answers


  1. Chosen as BEST ANSWER

    Utilizing HttpClientFactory along with its configured resilience features has effectively extended the timeout to approximately 2 minutes, a significant improvement from the initial 30-second threshold. However, despite this adjustment, the same exception continues to be raised.

    using Microsoft.Extensions.DependencyInjection;
    
    Console.WriteLine("Reading big request from api!"); 
    
    var services = new ServiceCollection();
    var builder = services.AddHttpClient<ApiClient>(client =>
    {
        client.BaseAddress = new({URL});
        client.Timeout = Timeout.InfiniteTimeSpan;
    });
    builder.Services.ConfigureHttpClientDefaults(http =>
    {
        http.AddStandardResilienceHandler(config =>
        {
            var timeSpan = TimeSpan.FromMinutes(10);
            config.AttemptTimeout.Timeout = timeSpan;
            config.CircuitBreaker.SamplingDuration = timeSpan * 2;
            config.TotalRequestTimeout.Timeout = timeSpan * 3;
        });
    });
    
    var httpClientFactory = services.BuildServiceProvider().GetRequiredService<IHttpClientFactory>();
    var apiClient = new ApiClient(httpClientFactory.CreateClient(nameof(ApiClient)));
    
    Console.WriteLine(await apiClient.GetAsync());
    

  2. If the you assuming it is because of HttpClient() timeout:

    The issue might be related to the default timeout settings of the HttpClient class in .NET, rather than Docker settings. By default, the HttpClient class has a timeout of 100 seconds for both the entire request and reading the response stream.

    To increase the timeout for your HttpClient requests, you can set the Timeout property of the HttpClient instance

    var client = new HttpClient();
    client.Timeout = TimeSpan.FromMinutes(5); // Set timeout to 5 minutes (adjust as needed)
    

    Additionally, you can also consider configuring Docker to increase its network-related timeouts, but since the issue is related to the HttpClient in your application, adjusting the timeout within your code should suffice

    Ref : https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.timeout?view=net-8.0

    If not:

    • Check for Server Logs
    • Test your API with tool like cURL or Postman to manually make requests to the API endpoint that your console application is trying to access. This can help determine if the issue is specific to the console application or if it also occurs with other HTTP clients
    • Inspect Network Traffic
    • Review Server Configuration
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search