I have the following piece of code in a domain service:
HttpClient client = new HttpClient();
client.GetAsync("http://somewebsite.com").Result;
Which works fine, I can place a break point on followed lines and they do hit and all is good. However, I have the same line of code in a nuget package installed in this very same project. There there's the exact same http call:
public class Client : Base
{
public Task<List<Stuff>> GetAsync()
{
return SendMessageAsync(HttpMethod.Get, "http://getstuff.com")
.ContinueWith(x => JsonConvert.DeserializeObject<List<StuffView>>(x.Result.Content.ReasAsStringAsync().Result);
}
}
public class Base
{
HttpClient client:
public Base(HttpClient client)
{
this.client = client:
}
protected async Task<HttpResponseMessage> GetMessageAsync(BttpMethod method, string url)
{
var request = CreateRequestAsync(method, url);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = await client.SendAsync(request); //line 1
return response; // Line 2
}
protected HttpRequestMessage CreateRequestAsync(HttpMethod method, string url)
{
var request = new HttpRequestMessage(method, url);
request.SetBearerToken(myAccessTokenProvider);
return request;
}
}
This is how my domain service is using the nuget package code:
var client = factory.Create();
client.GetAsync().Result;
Debugging this code shows that inside Base class line 1 is hit but line 2 never does. Searching on internet it seems like a thread deadlock issue. So my question is, assuming it's a deadlock, why the heck the first http call in domain service works but not the second one that uses the nuget package code?!
HttpClient deadlock issue: HttpClient.GetAsync(...) never returns when using await/async