I am trying to wrap my head around synchronously calling async functions and deadlock.
In the below example I am blocking an async call which internally uses ConfigureAwait(false) which as far as I can tell should prevent deadlock. The first call to the web service does not deadlock. However, the second one that happens within a continuation that syncs back to the UI thread deadlocks.
GetBlocking_Click is a click event in a WPF app so both the first and the second request happen on the UI thread.
private void GetBlocking_Click(object sender, RoutedEventArgs e)
{
const string uri = "http://www.mywebservice.com";
var example1 = GetAsync(uri).Result;
Task.FromResult(true)
.ContinueWith(t =>
{
var example2 = GetAsync(uri).Result;
},
TaskScheduler.FromCurrentSynchronizationContext()).Wait();
}
private async Task<string> GetAsync(string url)
{
using (var client = new HttpClient())
{
var responseMessage = await client.GetAsync(url).ConfigureAwait(false);
return await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
}
}
Can you please explain what is the difference between these 2 calls?