Click here to Skip to main content
Click here to Skip to main content

Asynchronous Controller in ASP.NET MVC

, 11 Jan 2013
Rate this:
Please Sign up or sign in to vote.
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

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)

Share

About the Author

No Biography provided

Comments and Discussions

 
GeneralMy vote of 2 Pinmembermorzel16-Dec-13 11:07 
QuestionThank you PinmemberBjarne Havnen - CodePort AS10-Sep-13 7:57 
QuestionMessage Removed Pinmembersarim ch11-Jan-13 3:41 
AnswerRe: Nice post PinmemberKumar_Jitendra21-Sep-13 9:41 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 11 Jan 2013
Article Copyright 2013 by Kumar_Jitendra
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid