Asynchronous page feature application in asp.net 2.0

Source: Internet
Author: User

ASP.net 2.0 offers a number of new features, including declarative data binding and master pages, member and role management services, and more. But I think the best feature is the asynchronous page, and let me tell you why.

When ASP.net receives a request for a page, it extracts a thread from the thread pool and assigns the request to that thread. An ordinary (or synchronized) page preserves threads during the request, preventing the thread from being used to process other requests. If a synchronization request becomes an I/O binding (for example, if it calls a remote Web service or queries a remote database and waits for the call to return), the thread that is assigned to the request is in a pending state before the call returns. This affects scalability because the thread pool has a limited number of threads available. If all request processing threads are blocked for the I/O operation to complete, the other requests are queued up for the thread to be freed. The best scenario is a reduction in throughput because the request waits a long time before it can be processed. The worst-case scenario is that the queue fills up and asp.net the subsequent request fails because of the 503 "Server unavailable" error.

Asynchronous pages provide an excellent solution for problems caused by I/O-bound requests. Page processing starts with thread pool threads, but when an asynchronous I/O operation begins to respond to a asp.net signal, the thread returns to the thread pool. When the operation completes, asp.net extracts another thread from the thread pool and completes processing of the request. Because thread pool threads are used more efficiently, scalability is increased. Threads that suspend waiting I/O completion are now available to service other requests. Direct beneficiaries are requests that do not perform long I/O operations and are therefore able to move quickly into and out of pipelines. Waiting for a long time to enter the pipeline can have a negative impact on the performance of such requests.

ASP.net 2.0 a few documents related to the Beta 2 asynchronous page infrastructure. Let's look at the prospect of an asynchronous page to make up for this. Keep in mind that this column covers beta versions of ASP.net 2.0 and. NET Framework 2.0.

Asynchronous pages in ASP.net 1.x

ASP.net 1.x does not support asynchronous pages in nature, but it can generate asynchronous pages through tough efforts and relentless innovation. For more overview information, see related information

The trick here is to implement IHttpAsyncHandler in a page's code-behind class, which prompts asp.net to process the request by invoking Ihttpasynchandler.beginprocessrequest instead of calling the page's Ihttphandler.processrequest method. Your BeginProcessRequest implementation can then start another thread. The thread calls base. ProcessRequest, which enables the page to enter its regular request processing lifecycle, which completes events such as Load and Render, but is not an exception on a ThreadPool thread. Also, BeginProcessRequest returns immediately after the new thread is started, allowing the thread that executes the beginprocessrequest to return to the thread pool.

This is the basic idea, but there are a lot of caveats in the details. Where you need to implement IAsyncResult and return it from BeginProcessRequest. This usually means creating a ManualResetEvent object and sending a signal to the ProcessRequest when it returns in the background thread. In addition, you must provide call base. The ProcessRequest thread. Unfortunately, most of the conventional techniques used to move work to background threads, including Thread.Start, ThreadPool.QueueUserWorkItem, and asynchronous delegates, are counterproductive in asp.net applications because they either "steal" threads from the thread pool , or there is a risk of unlimited thread growth. The correct asynchronous page implementation uses the custom thread pool, but custom thread pool classes are not easy to write.

It is not impossible to generate asynchronous pages in ASP.net 1.x, but it is somewhat tedious. After you try one or two times, you can't help thinking that there will be a better way. At present, this good method is ASP.net 2.0.

Asynchronous pages in ASP.net 2.0

ASP.net 2.0 greatly simplifies the way in which asynchronous pages are generated. The async= "true" attribute is introduced first using the @ Page directive of the page, as follows:

In the background, this notifies asp.net to implement IHttpAsyncHandler on the page. Next, you invoke the new Page.addonprerendercompleteasync method to register a Begin method and an end method early in the page's lifetime (for example, when Page_Load), as shown in the following code:

AddOnPreRenderCompleteAsync (
new BeginEventHandler(MyBeginMethod),
new EndEventHandler (MyEndMethod)
);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.