I have a long list of API URL calls in urlList
each of them returning quite an amount of JSON data. Some of these take milliseconds, others may well take several minutes to complete. That's why I set the timeout to 30 minutes. For some reason the HttpClient instance times out after just 5 minutes and I have no idea why. When I open the same URLs in a browser window or in Postman it takes between 8 and 20 minutes, but they will eventually load while my C# code (see below) throws these exceptions:
For some of the URLs I then get a CanceledException: Task CanceledException - System.Threading.Tasks.TaskCanceledException: A task was canceled. at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at ...
private async Task<string> ProcessUrls(urlList)
{
string userAndPasswordToken = Convert.ToBase64String(Encoding.UTF8.GetBytes(username + ":" + password);
ServicePointManager.ServerCertificateValidationCallback += this.ValidateRemoteCertificate;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
this.httpclient.DefaultRequestHeaders.Accept.Clear();
this.httpclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
this.httpclient.Timeout = TimeSpan.FromMinutes(30);
this.httpclient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization",$"Basic {userAndPasswordToken}");
SemaphoreSlim throttler = new SemaphoreSlim(initialCount: 20);
List<Task> requests = new List<Task>();
foreach (var url in urlList)
{
await throttler.WaitAsync();
requests.Add(
Task.Run(async () =>
{
try
{
result = await this.httpclient.GetAsync(url);
}
finally
{
throttler.Release();
}
}
)
);
}
}
UPDATE: I tried the same thing without the Semaphore, still, the same problem occurs - So I guess I can rule out that the Semaphore causes the issue.
What am I missing here? (Note: The APIs I am accessing are not under my control)
This is a .NET project (targetFramework net452), currently running in VS2019 on Win10.