Search code examples
c#dotnet-httpclient

HttpClient gets stuck on async task


I have a HttpClient and I'm pulling streams based on files on a remote server using the following code. When the thread gets to the third await, it pauses there for a long time. In case the size of the files is getting in the way they are 33347 kb, 123665 kb, and 178688 kb respectfully though I doubt this is the case because the last two are similar in size.

// a property in a class
private HttpClient HttpClient { get; set; } = new HttpClient() { Timeout = TimeSpan.FromMilliseconds(-1) };

// in a function in that class
using Stream stationsStream = await HttpClient.GetStreamAsync("https://eddb.io/archive/v6/stations.jsonl");
using Stream systemsStream = await HttpClient.GetStreamAsync("https://eddb.io/archive/v6/systems_populated.jsonl");
// always gets stuck on the third await no matter the order.
using Stream listingStream = await HttpClient.GetStreamAsync("https://eddb.io/archive/v6/listings.csv"); 

I haven't used the HttpClient class too much but some theories that came up are that the API only allows two connections or the HttpClient only can take two.


Solution

  • I needed to create the HttpClient with a HttpClientHandler and pass the HttpClientHandler into the HttpClient's constructor also changing the HttpClientHandler's MaxConnectionsPerServer to a larger number. For example:

    private HttpClient HttpClient { get; set; } = new HttpClient(Handler) { Timeout = TimeSpan.FromMilliseconds(-1) };
    private static HttpClientHandler Handler { get; set; } = new HttpClientHandler() { MaxConnectionsPerServer = 3 };