What Happens When HttpClient.PostAsync is Not Awaited

Viewed 127

I'm inspecting some code that seems to throw

System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host at times.

We use the library Polly for auto retries for HttpClient requests.

  • I'm starting to wonder if it has to do with the HttpClient.PostAsync not being awaited below?
  • Would that cause any connection exhaustion?

Any guidance would be appreciated.

var payload = JsonConvert.SerializeObject(data);
var response = await _pollyPolicy.ExecuteAsync(() => _httpClient.PostAsync(_endpoint, new StringContent(payload, Encoding.UTF8, "application/json")));

versus what I "think" it should be (note the async await added):

var payload = JsonConvert.SerializeObject(data);
var response = await _pollyPolicy.ExecuteAsync(async () => await _httpClient.PostAsync(_endpoint, new StringContent(payload, Encoding.UTF8, "application/json")));
1 Answers
Related