HttpClient 卡在异步任务上
HttpClient gets stuck on async task
我有一个 HttpClient,我正在使用以下代码基于远程服务器上的文件拉取流。当线程走到第三个await时,它会在那里暂停很长时间。如果文件的大小妨碍了它们分别为 33347 kb、123665 kb 和 178688 kb,但我怀疑是这种情况,因为最后两个文件的大小相似。
// 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");
我没有太多地使用 HttpClient class,但是一些理论认为 API 只允许两个连接或者 HttpClient 只能有两个。
我需要用 HttpClientHandler
创建 HttpClient
并将 HttpClientHandler
传递给 HttpClient
的构造函数,同时更改 HttpClientHandler
的MaxConnectionsPerServer
到更大的数字。例如:
private HttpClient HttpClient { get; set; } = new HttpClient(Handler) { Timeout = TimeSpan.FromMilliseconds(-1) };
private static HttpClientHandler Handler { get; set; } = new HttpClientHandler() { MaxConnectionsPerServer = 3 };
我有一个 HttpClient,我正在使用以下代码基于远程服务器上的文件拉取流。当线程走到第三个await时,它会在那里暂停很长时间。如果文件的大小妨碍了它们分别为 33347 kb、123665 kb 和 178688 kb,但我怀疑是这种情况,因为最后两个文件的大小相似。
// 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");
我没有太多地使用 HttpClient class,但是一些理论认为 API 只允许两个连接或者 HttpClient 只能有两个。
我需要用 HttpClientHandler
创建 HttpClient
并将 HttpClientHandler
传递给 HttpClient
的构造函数,同时更改 HttpClientHandler
的MaxConnectionsPerServer
到更大的数字。例如:
private HttpClient HttpClient { get; set; } = new HttpClient(Handler) { Timeout = TimeSpan.FromMilliseconds(-1) };
private static HttpClientHandler Handler { get; set; } = new HttpClientHandler() { MaxConnectionsPerServer = 3 };