This question is different from await vs Task.Wait - Deadlock?. That question deals with a case where (purportedly) await causes a deadlock, and .Wait doesn't. This question is the reverse. Additionally, there is disagreement between the question and answerer on the other question, so it cannot answer my question at all.
This causes a deadlock in the context of an ASP.Net request:
protected void TestBtnClick(object sender, EventArgs e)
{
DoSomethingAsync()
.Wait();
}
private async Task DoSomethingAsync()
{
await Task.Delay(2000)
.ConfigureAwait(continueOnCapturedContext: true);
}
My understanding is that the first calling method is waiting to marshall the original synchronisation context, and then the called method also waits to marshall the original context, thus causing the deadlock.
If this is the case, why doesn't the following also cause a deadlock?
protected async void TestBtnClickAsync(object sender, EventArgs e)
{
await DoSomethingAsync()
// ****** DIFFERENCE IS HERE: ******
.ConfigureAwait(continueOnCapturedContext: true);
}
private async Task DoSomethingAsync()
{
await Task.Delay(2000)
.ConfigureAwait(continueOnCapturedContext: true);
}
Note that both async calls explicitly request to continue on the original synchronisation context.