Click here to Skip to main content
15,881,938 members
Articles / Programming Languages / C# 3.5
Tip/Trick

Asynchronous Controller in ASP.NET MVC

Rate me:
Please Sign up or sign in to vote.
4.86/5 (13 votes)
11 Jan 2013CPOL2 min read 85.8K   12   7
The asynchronous controller enables you to write asynchronous action methods.

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.

When to use Asynchronous Controller

Asynchronous action methods are useful when an action must perform several independent long running operations. Suppose we have three operations which takes 500, 600 and 700 milliseconds. With the synchronous call, total response time would be slightly more than 1800 milliseconds. However, 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.

How to create Asynchronous Controller

  1. Inherits MVC controller with AsyncController instead of Controller.
  2. Asynchronous action methods are useful when an action must perform several independent long running operations. Suppose we have three operations which takes 500, 600 and 700 milliseconds. With the synchronous call, total response time would be slightly more than 1800 milliseconds. However, 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.

Using the code

C#
public class AsynchronuosTestingController : AsyncController
{
    private readonly object _lock = new object();
    public void IndexAsync()
    {
        AsyncManager.OutstandingOperations.Increment(2);
        Operation1();
        Operation2();
    }
    public ActionResult IndexCompleted(string Operation1, string Operation2)
    {
        ViewData["Operation1"] = Operation1;
        ViewData["Operation2"] = Operation2;
        return View("Index");
    }
    void Operation1()
    {
        lock (_lock)
        {
            AsyncManager.Parameters["Operation1"] = "Result1";
        }
        //last line of this method
    
        AsyncManager.OutstandingOperations.Decrement();
    }
    void Operation2()
    {
        lock (_lock)
        {
            AsyncManager.Parameters["Operation2"] = "Result2";
        }
        //last line of this method
    
        AsyncManager.OutstandingOperations.Decrement();
    }
}

You can see above in the sample code, we can store the results/values which we want to display on the views in the parameter collections. All the parameter values can be received with the parameters in the actionCompleted (InvokeCompleted) methods.

View Code

@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<p>@ViewData["Operation1"] </p>
<p>@ViewData["Operation2"]</p>
</div>
</body>
</html>

You may be not able to see the difference here but you can check with the long running process to feel the differences.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
PraiseSimple, clear and crisp code, explains the complete details. Thanks Pin
AmitMukherjee2-Nov-15 0:21
AmitMukherjee2-Nov-15 0:21 
QuestionMy vote is 2 Pin
reza assar9-May-15 23:59
reza assar9-May-15 23:59 
GeneralMy vote of 2 Pin
morzel16-Dec-13 11:07
morzel16-Dec-13 11:07 
QuestionThank you Pin
Bjarne Havnen - CodePort AS10-Sep-13 7:57
Bjarne Havnen - CodePort AS10-Sep-13 7:57 
AnswerRe: Nice post Pin
Kumar_Jitendra21-Sep-13 9:41
Kumar_Jitendra21-Sep-13 9:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.