Briefly explain to me about how should we work with asynchronous controller in asp.net MVC with example.
How Asynchronous Controllers work in asp.net MVC?
548
03-Feb-2020
Updated on 03-Feb-2020
Nishi Tiwari
03-Feb-2020A working of Asynchronous Controllers is used when users send a request to Asynchronous Controllers the asp.net will provide a thread from Thread pool to handle the request. The request is small then it will process it in normal way but in case if the processing is taking too long time to complete then it will return the process to thread pool for handling other request and whenever the asynchronous operation will complete, it notifies ASP.NET then asp.net gets a worker thread from the thread pool to process the remaining of the request, which includes rendering the response.
A convention required for following when using Asynchronous Controllers. For Asynchronous Controllers we have two methods:-
1. Async
2. Completed
Async
The async method will return void and it will start the asynchronous process. The syntax of the async method in asp.net MVC is shown below
Completed
The method is called when the asynchronous process is complete. The syntax of the Completed method in asp.net MVC is shown below
We will learn both these methods in detail with an example. In this example we used web service (Weatherservice) in IndexAsync method in that we created a proxy client which will take Country name as an input and will Return Cities then we will store cities in TempData["ListofCities"] when process will complete then it will call IndexCompleted() Method from which we will display result on view.