Asynchronous Controller In ASP.NET MVC
Asynchronous Controller - Introduction
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. If a request make a service call that require two seconds to complete it, the request will take two seconds whether it is performed synchronously or asynchronously. However, during asynchronous call, the server is not blocked from responding to the other requests.
Create Asynchronous Controller
- Inherits MVC controller with AsyncController instead of Controller.
- With the synchronous call, if the calls are made asynchronously (in parallel), total response time would be slightly more than 700 milliseconds, because that is the duration of longest task/operation.
This is a sample code to describe how an Asynchronous Controller will works.
Asynchronous actions are performed in the following scenarios :
- The operations are simple or short-running.
- Simplicity is more important than efficiency.
- The operations are primarily CPU operations instead of operations that involve extensive disk or network overhead. Using asynchronous action methods on CPU-bound operations provides no benefits and results in more overhead.
This figure shows the , From a higher level how ASP.NET MVC processes a request using the controller
When the browser passes a request, The controller will take care of the process ,the request is routes to the controller.The response is again back to browser after the process is done.