Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to create Asynchronous device Page in ASP.NET 4.5

In this tutorial we will make use of the latest ASP.NET techniques to come up with a asynchronous device page via assistance of Visual Studio Express.

The sample application that we are going to use here makes use of new async and awaits keywords and these are available in .NET 4.5 and Visual Studio 2012. This permits the complier to hold the responsibility of taking care of the complex transformations that are required for asynchronous programming. Also the compiler will allow you to write the code utilizing the C#'s synchronous control flow constructs. Hence the compiler on its own applies the transformations that are required to deploy call-backs. This is done so as to avoid blocking threads.

ASP.NET asynchronous pages needs to comprise of the Page directive and Async feature label set to “true”. The code listed below reflects the Page directive with the value of Async attribute set to true for the DeviceAsync.aspx page.

<%@ Page Async="true" Language="C#" AutoEventWireup="true" 
    CodeBehind="DeviceAsync.aspx.cs" Inherits="WebAppAsync.DeviceAsync" %>

The below code reflects the Device synchronous Page_Load method as well as the DeviceAsync asynchronous page. In case the browser supports the HTML5 <mark> element, the changes can be seen in DeviceAsync in yellow highlight.

protected void Page_Load(object sender, EventArgs e)
{
   var deviceService = new DeviceService();
   DeviceGridView.DataSource = deviceService.GetDevice();
   DeviceGridView.DataBind();
}

The below displays the asynchronous version:
protected void Page_Load(object sender, EventArgs e)
{
    RegisterAsyncTask(new PageAsyncTask(GetDeviceSvcAsync));
}

private async Task GetDeviceSvcAsync()
{
    var deviceService = new DeviceService();
    DeviceGridView.DataSource = await deviceService.GetDeviceAsync();
    DeviceGridView.DataBind();
}

The below lists the changes that were applied to permit DeviceAsync page to be asynchronous.

  • It is a must to set the Page directive Async attribute to "true".
  • The RegisterAsyncTask means is deployed to register an asynchronous task that comprises of the code which is operated asynchronously.
  • The new GetDeviceSvcAsync technique is labelled with the async keyword that informs the compiler to create callbacks for parts of the body and also to generate a Task of its own that is returned.
  • If you notice, we appened "Async" to the asynchronous method name though this is not required but it is the convention while you are writing asynchronous methods.
  • The return type of the Task defines the work in progress and offers the callers of the method with a handle via which to halt for the completion of the asynchronous operation. The return type of the new GetDeviceSvcAsync method is Task.
  • Web service call makes use of the await keyword.
  • The asynchronous web service API was known as GetDeviceAsync.

GetDeviceAsync is another asynchronous method that is called inside of the GetDeviceSvcAsync method body. Task<List<Device>> is immediately returned by a GetDeviceAsync that will finally complete on the availability of the data. For the reason that you would not want to proceed further before you have the device data, the code waits for the task making use of the await keyword. One can make use of the await keyword just in methods annotated with the async keyword.

Until we have the completion of the task, the thread is not blocked by the await keyword. It signs up the rest of the method as a callback on the task, and immediately returns. On the completion of the awaited task, that callback will be invoked thereby resuming the implementation of the method right where it left off.

The below code displays the GetDevice and GetDeviceAsync methods:

public List<Device> GetDevice()
{
   var uri = Util.getServiceUri("Device");
   using (WebClient webClient = new WebClient())
   {
        return JsonConvert.DeserializeObject<List<Device>>(
            webClient.DownloadString(uri)
        );
    }
}
public async Task<List<Device>> GetDevicesAsync()
{
    var uri = Util.getServiceUri("Devices");
    using (HttpClient httpClient = new HttpClient())
    {
        var response = await httpClient.GetAsync(uri);
        return (await response.Content.ReadAsAsync<List<Device>>());
    }
}

The asynchronous modifications that have been applied are same as those applied to the DevicesAsync above.

  • Async keyword was used to annotate the method signature, the return type was changed to Task<List<Device>>, and Async was appended to the method name.
  • The asynchronous HttpClient is utilized rather than synchronous WebClient class.
  • The await keyword was applied to the HttpClient GetAsync asynchronous method.

The below image reflects the asynchronous device view.

Device Asynchronous
Name Introduced Quantity
Device 1 05/05/2012 100
Device 2 05/06/2012 125
Device 3 05/07/2012 150
Elapsed time: 0.051

The browsers display of the device data is same as to the view that is generated by the synchronous call. Here the only difference is the asynchronous version may be more perform ant under major heavy loads.

Methods that are hooked up with RegisterAsyncTasks will be executed promptly after PreRender. One can also make use of async void page events directly as can be seen from the below code:

protected void Page_Load(object sender, EventArgs e) {
    await ...;
    // do work
}

The drawback to the async events is that the programmers now do not have the full access to the events when events are implemented. Example, in case both an .aspx and a .Master signifies Page_Load events and at least one of them is asynchronous, there is no guarantee of the execution.

Conclusion

Being a developer, it is recommended to apply the above stated process to come up with an asynchronous device page making use of the latest ASP.NET 4.5 technologies. Hope you liked the article.



Software Developer from India. I hold Master in Computer Applications Degree and is well versed with programming languages such as Java, .Net, C and C++ and possess good working knowledge on Mobile Platforms as well.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login