Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does an async method execute in ASP.NET (MVC)?

Many articles (e.g. this one) say the advantage of async methods in ASP.NET (MVC) is that they allow release of threads to the thread pool which allow other requests to be serviced. If async methods do not use thread pool threads, where do they execute and why?


1 Answers

The main use of async in this context is to wait for external resources - for example, databases (sql or no-sql), web APIs (http), etc. There is no thread required for these, since they are not CPU-based operations. The work is resumed at some point after the data becomes available. Consider:

var cust = await someApi.GetCustomerAsync();
var account = await anotherApi.GetAccount(cust.AccountId);
return View(account);

The await here represent out-of-process work - typically network. They don't "run" anywhere, because they are not CPU operations. When the place-holder tasks report completion, then the next part of the method can resume, typically via the captured sync-context.

like image 199
Marc Gravell Avatar answered Dec 07 '25 03:12

Marc Gravell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!