Sunday, 27 September 2015

Asynchronous Controller in ASP.NET MVC



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.




No comments:

Post a Comment