Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should you use Async Controllers in ASP.NET MVC?

Changing an ASP.NET MVC synchronous controller (Controller) to an asynchronous controller (AsyncController) seems like a trivial thing to do, but when should you do it?

Should I just make every controller async irrespective of its actions? What are examples of operations that would be improved if used in an asynchronous controller?

Taking the most trivial example: static html pages. So you have the most basic of controllers which simply returns a View from the Index action. Should this controller be changed to asynchronous i.e. now returning from IndexCompleted?

like image 259
Andreas Grech Avatar asked Feb 08 '11 09:02

Andreas Grech


People also ask

What is the use of async controllers in MVC?

The asynchronous controller enables you to write asynchronous action methods. It allows you to perform long running operation(s) without making the running thread idle. It does not mean it will take lesser time to complete the action.

Should I use async controller actions?

Calling an asynchronous controller action will not block a thread in the thread pool. Asynchronous actions are best when your method is I/O, network-bound, or long-running and parallelizable. Another benefit of an asynchronous action is that it can be more easily canceled by the user than a synchronous request.

What is the primary purpose of using an async controller?

The AsyncController class enables you to write asynchronous action methods. You can use asynchronous action methods for long-running, non-CPU bound requests. This avoids blocking the Web server from performing work while the request is being processed.

Why we use async and await in MVC?

Async, Await And Asynchronous Programming In MVC. Async keyword is used to call the function/method as asynchronously. Await keyword is used when we need to get result of any function/method without blocking that function/method.


2 Answers

I was reading this article recently. It think it summarizes what AsyncController are ment for.

like image 171
LeftyX Avatar answered Sep 25 '22 04:09

LeftyX


I know this is a old question but i struggled to get the answer , so heres my two cents.

Its like saying that if we do not have fever , should i still take a pill. You should use Asynch controller if you see thread starvation on your web server. IIS webserver maintains a pool of threads. So when any request comes in he picks up the thread from thread pool. If at a given moment all the threads from the pool are utilized and request comes in, that request goes in a waiting mode. This situation is termed as "Thread starvation". You can also watch this youtube video where i have demonstrated how MVC thread starvation looks like

http://www.youtube.com/watch?v=wvg13n5V0V0

Web Server Thread Use Diagram

When you make your controller as Asynch, it utilizes the thread, spawns the operation, and moves that thread back to the thread pool so it be can utilized for other requests coming in to the MVC application. Once the operation finishes he pulls back the thread from thread pool and displays the view.

like image 28
Shivprasad Koirala Avatar answered Sep 24 '22 04:09

Shivprasad Koirala