Asynchronous Controller in Asp.Net MVC 4 with Example

  Total Views : 2559
  Comments : 0
 
  Keywords : Asynchronous controller in asp.net mvc example, What is asynchronous controller in asp.net mvc, Using asynchronous controller in asp.net mvc application, Async controllers in asp.net mvc

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

 

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

 

If such large requests are send from users and all are long running process then server will be blocked then there will be no thread to handle this request and 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?

The working of Asynchronous Controllers is when user send request to Asynchronous Controllers the asp.net will provide a thread from Thread pool to handle request. If the request is small then it will process in normal way but in case if processing is taking too long time to complete then it will return process to thread pool for handling other request and whenever the asynchronous operation is complete, it notifies ASP.NET then 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 remmaining of the request, including rendering the response.

 

The convention required for 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 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 Completed method in asp.net mvc like as shown below 

  

Action Name Completed

We will learn these methods in details with simple Example. 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 process will complete then it will call IndexCompleted() Method from which we will display 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

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User