Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Asynchronous Controller in ASP.NET MVC

0.00/5 (No votes)
11 Jan 2013 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here