问题
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
?
回答1:
I was reading this article recently. It think it summarizes what AsyncController are ment for.
回答2:
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.
来源:https://stackoverflow.com/questions/4931592/when-should-you-use-async-controllers-in-asp-net-mvc