C#C
C#17mo ago
ShevaKadu

✅ HTTPClient's GetAsync errors and doesn't get caught

In this code
c#
private async Task<CheckResult> TestHTTP()
{
    ...
    // above code creates httpclient as client
    try
    {
(!)     response = await client.GetAsync(expectedURL);
    }
    catch (TaskCanceledException ex) when (ex.InnerException is TimeoutException)
    {
        client.Dispose();
        handler.Dispose();
        return CheckResult.TimedOut;
    }
    catch
    {
        client.Dispose();
        handler.Dispose();
        return CheckResult.TimedOut;
    }
    ...
}

client is a Httpclient which is supposed to timeout after a delay.
To test that, I tried getting it to access a URL that doesn't work.

client.GetAsync( ).Result throws a TaskCancelledException , which doesn't get caught

Adding:
This function is supposed to be used in multiple threads.
Maybe having the thread error out and shutdown serve as a "catch" block of sorts can work?
However that is not optimal

How fix???:kekw:
Was this page helpful?