HttpClient async methods (e.g. PostAsync, GetAsync etc.), if an exception is thrown, returns an AggregateException. Now an aggregate exception can contain a list of inner exceptions. My question is can someone provide an example where one of the async methods on an http client leads to more than one inner exception?
Is it safe to assume that although there is potentially a list of inner exceptions, in most cases you will only get one inner exception?
I'm aware of why they are being throw and how to handle it.
So to make this clearer, is it possible for a single call to an http client async method that throws an aggregate exception to have more than one exception in it's list of inner exceptions?
If you look inside
HttpClient.SendAsync(which is the inner method being used to send all requests), you'll see that theTaskbeing created is a simpleTaskCompletionSource<HttpResponseMessage>. Inside the calling method, it setsthis.SetTaskFaultedmultiple times, but always inside anif-elseblock. What could potentially happen is that whenSetTaskFaultedsets the exception, it could set off another exception:DisposeCancellationTokenAndTimerinternally disposes theCancellationToken, and inside thefinallyblock disposes the timer:The timer could potentially throw an exception from its
Disposemethod. Although im sure that's very rare.