Member-only story
What’s the fuss about ConfigureAwait in asynchronous C#
Unveiling the Hidden Mechanics of .NET and C#
Introduction
I admit, asynchronous and parallel programming in C# is hard. You can do the same thing in so many different ways… One can think the end result is similar, it might be hugely different under the hood which gets in your way most of the time.
“Use this here and don’t use that”. You hear that alot without knowing why and where, and you can’t even know that they are right or wrong!
Here, I am uncovering one of the most commonly misunderstood things in C# which is ConfigureAwait
:
What’s ConfigureAwait?
ConfigureAwait
in C# is like telling your async code whether it should keep running on the same thread that called it, or switch to another thread in the background. Let’s consider this example:
public async Task InsertClient(User u, CancellationToken token)
{
var client = u.GetInnerClient(); // Operation (1)
await Api.PutAsync(client, token); // Operation (2)
NotifyUser(u); // Operation (3)
}
Here’s what we’re doing:
- We’re receiving a
User
, mapping it somehow to aClient
. (Operation 1)