0
 var appendFileResponse = await Policy
            .HandleResult<HttpResponseMessage>(message => !message.IsSuccessStatusCode)
            .WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)))
            .ExecuteAsync(async () => await httpClient.SendAsync(request).ConfigureAwait(true))
            .ConfigureAwait(true);

 if (appendFileResponse.StatusCode != System.Net.HttpStatusCode.Accepted)
 {
            throw new ApplicationException($"Failed to append file");
 }

I want to make sure appendFileResponse is completed before doing the If check and some other operations.

Should I add ConfigureAwait(true) in the httpClient.SendAsync call and/or .ConfigureAwait(true) in the await Policy?

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Sky W
  • 1
  • 2
  • Be careful with retrying when appending files, you could end up in a place where the request was processed successfully on the server but you never received a response. Polly retry policy will kick in and send the request again, resulting in a duplicate append. A bit more engineering may be required to make sure this action is indempotent. – Eric Walter Oct 11 '22 at 17:27

1 Answers1

2

ConfigureAwait has no effect on what order things run in. The fact that you're using await means that the code will "asynchronously wait" at that point.

ConfigureAwait just determines which context your code is resuming on: a captured context (the default), or the thread pool context (if you pass false).

The if is run after all the Polly retries regardless of whether or not you use ConfigureAwait.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810