There’s 2 likely reasons that a TaskCanceledException
would be thrown:
- Something called
Cancel()
on theCancellationTokenSource
associated with the cancellation token before the task completed. - The request timed out, i.e. didn’t complete within the timespan you specified on
HttpClient.Timeout
.
My guess is it was a timeout. (If it was an explicit cancellation, you probably would have figured that out.) You can be more certain by inspecting the exception:
try
{
var response = task.Result;
}
catch (TaskCanceledException ex)
{
// Check ex.CancellationToken.IsCancellationRequested here.
// If false, it's pretty safe to assume it was a timeout.
}