The reason you see this is because your async void method runs in fire and swell mode, so you see the value returned by setting the response message, not the PostAsync response.
Do not expose one class HttpResponseMessage , it may be deprecated if it is called simultaneously in the same instance. Instead, return a new instance each time:
public async Task<HttpResponseMessage> SendRequestAsync(string adaptiveUri, string xmlRequest) { using (HttpClient httpClient = new HttpClient()) { StringContent httpConent = new StringContent(xmlRequest, Encoding.UTF8); HttpResponseMessage responseMessage = null; try { responseMessage = await httpClient.PostAsync(adaptiveUri, httpConent); } catch (Exception ex) { if (responseMessage == null) { responseMessage = new HttpResponseMessage(); } responseMessage.StatusCode = HttpStatusCode.InternalServerError; responseMessage.ReasonPhrase = string.Format("RestHttpClient.SendRequest failed: {0}", ex); } return responseMessage; } }
And when you call it, correctly await it:
return await restHttpClient.SendRequestAsync(adaptiveUri, xmlRequest);
source share