Since you are already expecting HttpResponseMessage , a simple (and consistent) solution is to return a Task<HttpResponseMessage> .
var x = await POSTDataHttpContent("test", "http://api/"); public async Task<HttpResponseMessage> POSTDataHttpContent( string jsonString, string webAddress) { using (HttpClient client = new HttpClient()) { StringContent stringContent = new StringContent(jsonString); HttpResponseMessage response = await client.PostAsync( webAddress, stringContent); Console.WriteLine("response is: " + response); return response; } }
However, you also need to make sure your test setup is correct. You cannot correctly call the asynchronization method from a synchronous test. Instead, mark your async test and expect the method you are calling. In addition, your test method should be marked as async Task , since neither MS Runner nor other tools (NCrunch, NUnit) will correctly process the asynchronous test method:
[TestMethod] public async Task TestAsyncHttpCall() { var x = await POSTDataHttpContent("test", "http://api/"); Assert.IsTrue(x.IsSuccessStatusCode); }
source share