Powered by Blogger.

Saturday, March 1, 2014

await and async - Task-Based Asynchronous Modules and Handlers in ASP.NET 4.5:

In .NET 4.0 an  asynchronous programming concept has been introduced named as task. Tasks are represented by the Task type and belongs to System.Threading.Tasks namespace. The .NET Framework 4.5 builds on this with compiler enhancements that make working with Task objects simple. In the .NET Framework 4.5, the compilers support two new keywords: await and async. The await keyword is syntactical shorthand for indicating that a piece of code should asynchronously wait on some other piece of code. The async keyword represents a hint that you can use to mark methods as task-based asynchronous methods.

Also check out:

- Constructor and Destructors in C#.Net

- Lock and Monitor statement in C#.Net

- Case statement in SQL Server 2008

The combination of await, async, and the Task object makes it much easier for you to write asynchronous code in .NET 4.5. ASP.NET 4.5 supports these simplifications with new APIs that let you write asynchronous HTTP modules and asynchronous HTTP handlers using the new compiler enhancements.

Asynchronous HTTP modules
Suppose that you want to perform asynchronous work within a method that returns a Task object. The following code snippet defines an asynchronous method that makes an asynchronous call to download the aspdotnethelp page. The  async keyword in the method signature  says that  the method is  a task-based asynchronous method and the await  keyword says that asynchronously wait and call to the method DownloadStringTaskAsync.

private async Task
ScrapeHtmlPage(object caller, EventArgs e)
        WebClient wc = new WebClient();
        var result = await wc.DownloadStringTaskAsync("http://www. aspdotnethelp.com");
        // Do something with the result

The .NET Framework will automatically handle unwinding the call stack while waiting for the download to complete, as well as automatically restoring the call stack after the download is done.

You can use this asynchronous method in an asynchronous ASP.NET HTTP module. ASP.NET 4.5 includes a helper method  called EventHandlerTaskAsyncHelper and a new delegate type named TaskEventHandler .The TaskEventHandler delegate is used to integrate task-based asynchronous methods with the older asynchronous programming model exposed by the ASP.NET HTTP pipeline. This example shows how:

public void Init(HttpApplication context)
   // Wrap the Task-based method so that it can be used with
   // the older async programming model.
   EventHandlerTaskAsyncHelper helper =
           new EventHandlerTaskAsyncHelper(ScrapeHtmlPage);

           // The helper object makes it easy to extract Begin/End methods out of
           // a method that returns a Task object. The ASP.NET pipeline calls the
           // Begin and End methods to start and complete calls on asynchronous
           // HTTP modules.
                   helper.BeginEventHandler, helper.EndEventHandler);

Asynchronous HTTP handlers
The traditional way of writing asynchronous handlers in ASP.NET is through the IHttpAsyncHandler interface. ASP.NET 4.5 introduces an asynchronous base type abstract class named  HttpTaskAsyncHandler. You can write asynchronous handlers easily  by deriving this  base class. Since this is an abstract class  you need to override the method  named ProcessRequestAsync. Internally ASP.NET takes care of integrating the return signature (a Task object) of ProcessRequestAsync with the older asynchronous programming model used by the ASP.NET pipeline.

The following code snippet  shows how you can use Task and await as part of the implementation of an asynchronous HTTP handler:

public class MyAsyncHandler : HttpTaskAsyncHandler
        // ASP.NET automatically takes care of integrating the Task based override
        // with the ASP.NET pipeline.
        public override async Task ProcessRequestAsync(HttpContext context)
           WebClient wc = new WebClient();
           var result = await
           // Do something with the result


Post a Comment