The Asynchronous Controller is the feature added in ASP.Net MVC 4.0 which enables to write the asynchronous action methods. Asynchronous controller allows an operation to get performed without making the working thread idle.

When an asynchronous action is invoked, the following steps occur:

  1. The Web server gets a thread from the thread pool (the worker thread) and schedules it to handle an incoming request. This worker thread initiates an asynchronous operation.
  2. The worker thread is returned to the thread pool to service another Web request.
  3. When the asynchronous operation is complete, it notifies ASP.NET.
  4. The Web server 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 remainder of the request, including rendering the response.
Asynchronous Controller in ASp.Net MVC 4.0


Difference:

In Synchronous operation , the working thread is dedicated to the single operation and even if the thread is idle (eg: waiting for the response from server , or for some I/O or network operation required to complete the desired operation) the working thread cannot be involved to serve some other process. In Asynchronous operation this working thread can be used to serve other process or operation. 

So now comes the question , when to use Synchronous Controller and when to use Asynchronous controller in ASP.Net MVC 4.0 ?

In general, use synchronous pipelines when the following conditions are true:

  • The operations are simple or short-running.
  • Efficiency is secondary concern.
  • The operations are primarily CPU operations instead of operations that involve extensive disk or network overhead.
While it will be better to use Asynchronous controller if following conditions hold true:

  • The operations are network-bound or I/O-bound instead of CPU-bound.
  • Testing shows that the blocking operations are a bottleneck in site performance and that IIS can service more requests by using asynchronous action methods for these blocking calls.
  • Parallelism is more important than simplicity of code.
  • You want to provide a mechanism that lets users cancel a long-running request.

Converting Synchronous Action Methods to Asynchronous Action Methods


Following is the example of synchronous action method and the its asynchronous equivalent version.

Synchronous Controller:

 public class TestController : Controller
    {
        public ActionResult Index()
        {
            return View();

        }
    }
Asynchronous variant of above operation:

 public class TestController : AsyncController
  {
        public void IndexAsync()
        {
           // Some processing
        }
public ActionResult IndexCompleted()
        {
            return View();
        }
 }

Steps:

  • Synchronous Controllers are the classes derived from the Controller class to implement an AsyncController instead of deriving the controller from Controller, derive it from AsyncController class. Controllers that derive from AsyncController enable ASP.NET to process asynchronous requests, and they can still service synchronous action methods.
  • Corresponding to the synchronous action method in Synchronous controller you need to create two methods for the action in asynchronous controller.First method that initiates the asynchronous process must have a name that consists of the action and the suffix "Async". The other method that is invoked when the asynchronous process finishes (the callback method) must have a name that consists of the action and the suffix "Completed".

In the above sample example, the Index action has been turned into two methods in asynchronous controller: IndexAsync and IndexCompleted.
The IndexAsync method returns void while the IndexCompleted method returns an ActionResult instance. Although the action consists of two methods, it is accessed using the same URL as for a synchronous action method (for example, Controller/Index).

The parameters that are passed to IndexAsync use the normal parameter binding mechanisms. The parameters that are passed to IndexCompleted use the Parameters dictionary.

Note the following about asynchronous action methods:
  • If the action name is Sample, the framework will look for SampleAsync and SampleCompleted methods.
  • View pages should be named Sample.aspx rather than SampleAsync.aspx or SampleCompleted.aspx. (The action name is Sample, not SampleAsync.)
  • A controller cannot contain an asynchronous method named SampleAsync and a synchronous method named Sample. If it does, an AmbiguousMatchException exception is thrown because the SampleAsync action method and the Sample action method have the same request signature.
Reference: MSDN

2 comments:

  1. public void IndexAsync()
    {
    return View();
    }
    ??????

    How can a method marked "void" return anything?
    Please clarify.

    ReplyDelete

 
Top