I have the following code that is supposed to call the ExecuteAsync function from Polly. However, after return objectResponse_.Object;
the call does not return to calling function and basically gets lost (events show thread has exited with code 0). I am sure I am doing something wrong with the await async but I can't tell what.
BTW, the Parallel Stacks shows
public async Task < T > SendRequestAsync < T > (HttpRequestMessage request, CancellationToken cancellationToken, bool someBool = true) {
var retryPolicy = Policy.Handle < Exception > ().Or < HttpRequestException > ().RetryAsync(3, onRetry: (exception, retryCount, context) =>{
_logger.Log($"API call failed. Doing retry attempt {retryCount}");
});
await retryPolicy.ExecuteAsync(async() =>{
using(var response = await _client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false)) {
var headers = System.Linq.Enumerable.ToDictionary(response.Headers, h =>h.Key, h =>h.Value);
if (response.Content ? .Headers != null) {
foreach(var item in response.Content.Headers) {
headers[item.Key] = item.Value;
}
}
var status = (int) response.StatusCode;
switch (status) {
case 200:
case 201:
case 204:
{
if (someBool) {
var objectResponse_ = await ReadObjectResponseAsync < T > (response, headers, cancellationToken).ConfigureAwait(false);
if (objectResponse_.Object == null) {
throw new MyException("Response was null which was not expected.", status, objectResponse_.Text, headers, null);
}
return objectResponse_.Object;
}
else {
var objectResponse_ = await ReadObjectResponseAsync < T > (response, headers, cancellationToken);
if (objectResponse_.Object != null) return objectResponse_.Object;
else return
default (T);
}
}
case 404:
{
if (someBool) {
var objectResponse_ = await ReadObjectResponseAsync < T > (response, headers, cancellationToken).ConfigureAwait(false);
return objectResponse_.Object;
}
var responseText = (response.Content == null) ? string.Empty: await response.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new MyException("Not Found", status, responseText, headers, null);
}
case 500:
{
if (someBool) {
var objectResponse_ = await ReadObjectResponseAsync < T > (response, headers, cancellationToken).ConfigureAwait(false);
return objectResponse_.Object;
}
var objectResponse = await ReadObjectResponseAsync < MyResponse > (response, headers, cancellationToken).ConfigureAwait(false);
if (objectResponse.Object == null) {
throw new MyException("Response was null which was not expected.", status, objectResponse.Text, headers, null);
}
throw new MyException < MyResponse > ("Internal Server Error", status, objectResponse.Object.Message, headers, objectResponse.Object, null);
}
default:
{
var responseData = response.Content == null ? null: await response.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new MyException("The HTTP status code of the response was not expected (" + status + ").", status, responseData, headers, null);
}
}
}
});
return
default (T);
}