Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

.NET

AJAX and Asynchronous Pages


One of the biggest advantages of ASP.NET AJAX is the ability to call server-side methods from within the client browser using JavaScript. Instead of posting a form to get back a brand new version of the current page, you can now take control yourself of the operations and invoke a specific method defined on the same server as the application. Input parameters for the method, and any return values, are serialized back and forth. The ASP.NET AJAX infrastructure takes care of data transfer and serialization on both the client and the server. You can define the server API that the client can invoke either using a specialized ASP.NET Web service or using static methods defined on the page's code-behind class.

The method call is executed asynchronously with respect to the client. This fact has a few positive repercussions on the page.

First and foremost, the page is not frozen during the execution of the method. The user can continue his interaction with the page and type in new data or even command a second method call. Good feedback and progress screens can be displayed to the user. The feedback screen is not limited to static text and markup. You can use marquees as well as animated GIFs. Because the current page is not killed waiting for a brand new one to be downloaded, the background thread that animates the image is still alive and can continue doing its own job. When the remote method invocation is completed and results are ready to be processed on the client, a user-defined JavaScript callback is called to merge received data with the current page tree. It looks like a simple and effective model with no downsides. Is it really true? Let's find out.

It is essential to notice that an AJAX asynchronous call is asynchronous only with the respect to the client browser. As far as the ASP.NET runtime engine is concerned, it remains a synchronous call. Is this a problem? It can become a problem; and a big one indeed. Let's examine what happens internally when a server method is invoked through a wrapper Web service.

When you expose the client-callable server API, you create an ASMX Web service. The ASP.NET AJAX framework then creates a JavaScript proxy class for the service. Whenever you call a method on the proxy class, a request for the ASMX resource hits the Web server. In the end, the ASP.NET runtime receives a request for an ASMX resource. In the ASP.NET runtime, these requests can only be served synchronously. This means that the ASP.NET pooled thread in charge of the request can't do much more than waiting for the method to terminate. If the Web service method takes a while, the thread is blocked. When too many threads get blocked, you have a potential problem of scalability. An asynchronous HTTP handler for ASMX resources is not in ASP.NET 2.0. You can write your own, but it wouldn't be a picnic.

ASP.NET AJAX is not necessarily a friendly framework if you have strong scalability requirements and the need to invoke lengthy server methods. Solutions? The most obvious is avoiding the method call from the client. Make a postback, or even a partial postback, and mark the ASP.NET page as asynchronous. ASP.NET pages can be processed asynchronously on the server; ASP.NET Web services cannot.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.