I have an async method that works when used with the .Result property, but not when using the await keyword. I thought using await instead of result was correct. Am I wrong?
// doesn't work - hangs
protected async Task<T> GetAsync<T>(string uri) =>
await ParseJson<T>(await Client.GetAsync(uri));
// works
protected async Task<T> GetAsync<T>(string uri) =>
await ParseJson<T>(Client.GetAsync(uri).Result);
The Client in the above code is a HttpClient, and the ParseJson method is as follows:
protected async Task<T> ParseJson<T>(HttpResponseMessage response) =>
JsonConvert.DeserializeObject<T>(await response.Content.ReadAsStringAsync(), Settings);
The code is being run in an asp.net mvc application if that makes any difference.
Update Fixed. Thanks to: @Igor - You were correct, the MVC controllor action calling the method was not asynchronous and was therefore using .Result instead of await. @Panagiotis - I've split my code out as you suggested to make it more debuggable.