ASP.NET MVC4 Async controller - Why to use?

12,984

The point of the await keyword is to let you work with asynchronous operations without writing ugly callbacks.

Using asynchronous operations helps avoid wasting thread pool threads.

Explanation

ASP.Net runs all of your code in threads from the managed thread pool.
If you have too many slow requests running at once, the thread pool will get full, and new requests will need to wait for a thread to get free.

Frequently, however, your requests are slow not because they're doing computation (compute-bound), but because they're waiting for something else, such as a hard disk, a database server, or an external webservice (IO- or network-bound).

There is no point in wasting a precious threadpool thread simply to wait for the external operation to finish.

Asynchronous operations allow you to start the operation, return your thread to the pool, then "wake up" on a different thread pool thread when the operation is finished.
While the operation is running, no threads are consumed.

Share:
12,984
DorR
Author by

DorR

Updated on July 08, 2022

Comments

  • DorR
    DorR almost 2 years

    I am trying to understand why and when should I use an async controller action. Eventually, when I use await in it, it will wait for the operation to complete in order to return the View.

    For example

    public async Task<ActionResult> TryMe()
    {
       await SomeActionAsync();
       return View();
    }
    

    In this case if I use the async or not using the async, the Action will take the same time to execute.

    If I am not trying to run at least 2 slow operations (that are not dependent on each other) in parallel, I don't see any reason to use an async controller action.

    Please correct me if I'm wrong. I think I'm missing something here.

  • DorR
    DorR about 11 years
    So, what you are saying is that I should use async methods (for operation as you said (DB, filesystem, WS etc...) just for freeing up the thread pool? Cause execution time will stay the same in both ways. Is it best practice to always use async when I try to run operations like this?
  • SLaks
    SLaks about 11 years
    @DorR: Yes. Note that not all operations will necessarily have async implementations. Most non-CPU-bound methods in .Net 4.5 now have async counterparts, but 3rd-party libraries are less likely to.
  • Erik Schierboom
    Erik Schierboom about 11 years
    There is an excellent talk from Steve Sanderson showing off the async features in ASP.NET: channel9.msdn.com/Events/TechDays/Techdays-2012-the-Netherla‌​nds/… This presentation also clearly shows the performance benefit one might get from using async methods.
  • Persian.
    Persian. about 10 years
    So using of this keyword is very good in applications. in small and large. correct?
  • Tim
    Tim over 9 years
    There were really two questions here: "why" and "when". This answer does a good job on the first. It would be helpful if someone answer the second. For example, should it be used any time it's practical to do so, or should it be used only e.g. for large applications, views that return large amounts of data, views that are compute bound, etc?
  • SLaks
    SLaks over 9 years
    @Tim: You should use async only when you have non-blocking (non-compute-bound) work.