Based on numerous books and blogs including this excellent one here, it is clear that when one writes a dll library exposing helper async methods i.e. the wrapper methods, it is generally considered a best practice to internally complete the I/O task of actual async methods on a threadpool thread like so (pseudo code shown below for brevity and I'm using HttpClient
as an example)
public Async Task<HttpResponseMessage> MyMethodAsync(..)
{
...
var httpClient = new HttpClient(..);
var response = await httpClient.PostAsJsonAsync(..).ConfigureAwait(false);
...
return response;
}
The key here is the usage of ConfigureAwait(false)
so that IO task completion occurs on a threadpool thread instead of on the original thread context, thereby potentially preventing deadlocks.
My question is from the perspective of a caller. I'm particularly interested in a scenario where there are layers of method calls between the caller and the above method call, as the following example shows.
CallerA -> Method1Async -> Method2Async -> finally the above MyMethodAsync
Is it enough to have ConfigureAwait(false)
on the final method only or should one also ensure Method1Async
and Method2Async
also internally call their async methods with ConfigureAwait(false)
?
It seems silly to have it included on all these intermediary methods, especially if Method1Async
and Method2Async
are simply overloads that end up calling MyMethodAsync
.
Any thoughts, please enlighten us!
Updated with Example So if I have a library with the following private async method,
private async Task<string> MyPrivateMethodAsync(MyClass myClass)
{
...
return await SomeObject.ReadAsStringAsync().ConfigureAwait(false);
}
should I make sure the following public overloaded methods both also include ConfigureAwait(false) as shown below?
public async Task<string> MyMethodAsync(string from)
{
return await MyPrivateMethodAsync(new (MyClass() { From = from, To = "someDefaultValue"}).ConfigureAwait(false);
}
public async Task<string> MyMethodAsync(string from, string to)
{
return await MyPrivateMethodAsync(new (MyClass() { From = from, To = to }).ConfigureAwait(false);
}