4

I am working on ASP.NET Web API 2 Application. In the application we are using for every async request:

ConfigureAwait(false);

I cannot really understand what does it mean. I looked on the internet but still don't understand what it exactly does?

kuskmen
  • 3,648
  • 4
  • 27
  • 54
Samy Sammour
  • 2,298
  • 2
  • 31
  • 66
  • 2
    read this [blog](https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html). – Soheil Alizadeh Aug 18 '17 at 12:43
  • so it means, it continues the context but on another random thread not waiting the same thread, correct? – Samy Sammour Aug 18 '17 at 13:14
  • Possible duplicate of [Best practice to call ConfigureAwait for all server-side code](https://stackoverflow.com/questions/13489065/best-practice-to-call-configureawait-for-all-server-side-code) – kuskmen Aug 18 '17 at 14:13

1 Answers1

4

To understand it, you need to understand what is Synchronization Context and Threading Models.

From practical perspective lets take a look at canonical example. You are developing GUI application (Win Forms or WPF). You have dedicated UI thread. You must update UI on UI thread and do not block UI thread by calculations or waiting some response.

ConfigureAwait have sense with async/await. By default, code after await will run on captured UI thread,

await DoAsync();
//UI updating code goes here. Will run on UI thread

Code after await will run on thread pool, if you specify ConfigureAwait(false)

await DoAsync().ConfigureAwait(false);
//CPU intensive operation. Will run on thread pool

You do not have dedicated UI thread, as far as you have ASP.NET application. But Synchronization Context is important too. Understanding the SynchronizationContext in ASP.NET will give you details.

Evgeni Lipatov
  • 326
  • 3
  • 12
  • okay thank you. clear. but is it really important in ASP.NET Web Api 2 application? – Samy Sammour Aug 18 '17 at 14:25
  • 2
    ASP.NET threading model is more complex. But basically, there is no strict rule to use ConfigureAwait(false). The default behavior is what most likely are you expecting. It can be used for performance tuning. However, ConfigureAwait(false) considered be a good practice in library code (supposing it can be used outside ASP.NET) – Evgeni Lipatov Aug 18 '17 at 15:23