Asynchronous Controller in Asp.Net MVC 4 with Example

In today's world, most of the application work based on Synchronous programming in which the client sent a request to the server and server process the task and sent a response back to the client. The synchronous controllers work well for the concept, but it cannot handle the long-running processes.

 

When a user sends a request to the server, the ASP.NET will provide one of the threads from the thread pool to handle the request. If the request process takes less time to complete, it provides fast output to the client, but when the request takes more time for the process, it will block the thread, and this thread will not be available to another request to handle.

 

If such large requests are sent from users, and all are long-running processes, then the server will be blocked, then there will be no thread to handle this request, and the server will fire Error message “Server too Busy”. To handle this, we need to use Asynchronous Controllers in asp.net mvc.

How Asynchronous Controllers work?

When users send requests to Asynchronous controllers, the asp.net will provide a thread from the Thread pool to handle requests. If the request is small, it will process normally, but if the process takes too long time to complete, it will return the process to a thread pool for handling other requests, and it notifies ASP.NET whenever the asynchronous operation is complete. The asp.net gets a worker thread from the thread pool (which might be a different thread from the thread that started the asynchronous operation) to process the remaining of the request, including rendering the response.

 

The convention is required to follow when using Asynchronous Controllers. We have 2 Methods of Asynchronous controllers.

  1. Async
  2. Completed

Async

The async method will return void, and it starts the asynchronous process. The syntax of the async method in asp.net mvc like as shown below.

 

Action Name Async

Completed

This method is called when the asynchronous process is complete. The syntax of the Completed method in asp.net mvc like as shown below.

  

Action Name Completed

We will learn these methods in detail with simple Examples. In this example, we used web service (Weatherservice) in IndexAsync method in which we created a proxy client which will take Country name as input and will Return Cities then we will store cities in TempData["ListofCities"] when the process completes then it will call IndexCompleted() Method from which we will display the result on view.

  

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

 

namespace MvcAttribute.Controllers

{

public class Default2Controller : AsyncController

{

public void IndexAsync()

{

Weatherservice.GlobalWeatherSoapClient objservice = new Weatherservice.GlobalWeatherSoapClient();

TempData["ListofCities"] = objservice.GetCitiesByCountry("India");

}

public ContentResult IndexCompleted()

{

string Result = TempData["ListofCities"].ToString();

return Content(Result);

}

}

}

When to Use Asynchronous Controllers?

  • For long ­running operations
  • For web­services relate operations