This only occurs under heavy load so I can't put up a simple example. This is for a system test so I'm trying to avoid await and instead I'm creating 2,000 requests and then polling for when the responses complete.
The problematic code is:
Task<HttpResponseMessage> response = responseStatus.Item1;
if (!response.IsCompleted)
continue;
HttpResponseMessage result = response.Result;
Where the call to Result throws the following:
System.AggregateException: One or more errors occurred.
StackTrace: at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task`1.get_Result()
at SystemTests.V2.LoadTests.Test1000Requests() in C:\git\Jenova\restfulengine\SystemTests.V2\LoadTests.cs:line 121
INNER EXCEPTION: System.Threading.Tasks.TaskCanceledException: A task was canceled.
StackTrace: System.AggregateException: One or more errors occurred. ---> System.Threading.Tasks.TaskCanceledException: A task was canceled.
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task`1.get_Result()
at SystemTests.V2.LoadTests.Test1000Requests() in C:\git\Jenova\restfulengine\SystemTests.V2\LoadTests.cs:line 121
---> (Inner Exception #0) System.Threading.Tasks.TaskCanceledException: A task was canceled.<---
2020-04-26 13:36:06,370 [MSTestAdapter Thread] WARN SystemTests.V2.LoadTests - v2/reports/19ea788d-ceb8-4a81-a974-f597494609dd failed (will retry)System.AggregateException: One or more errors occurred. ---> System.Threading.Tasks.TaskCanceledException: A task was canceled.
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task`1.get_Result()
at SystemTests.V2.LoadTests.Test1000Requests() in C:\git\Jenova\restfulengine\SystemTests.V2\LoadTests.cs:line 121
---> (Inner Exception #0) System.Threading.Tasks.TaskCanceledException: A task was canceled.<---
Task.IsCompleted is "true if the task has completed (that is, the task is in one of the three final states: RanToCompletion, Faulted, or Canceled); otherwise, false."
So if the HTTP request fails in a way that doesn't create a usable HttpResponseMessage
you can't access the Result. So check the Task.Status to see which it is.