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
?
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.
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.
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.
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.
I was reading this article recently. It think it summarizes what AsyncController are ment for.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With