Asynchronous page (from msdn) in ASP. NET 2.0)

Source: Internet
Author: User
Asynchronous pages in ASP. NET 2.0 released on: 2006-4-19 | updated on: 2006-4-19

Download the code in this article:Wickedcode0510.exe(123kb)

Content on this page
Asynchronous pages in ASP. NET 1.x
Asynchronous pages in ASP. NET 2.0
Asynchronous Data Binding
Asynchronously call Web Services
Asynchronous task
Wrap it

ASP. NET 2.0 provides a large number of new features, including declarative Data Binding and master pages, member and role management services. But I think the best feature is the asynchronous page. Let me tell you the reason.

When ASP. NET receives a page-based request, it extracts a thread from the thread pool and distributes the request to the thread. A normal (or synchronous) page retains the thread during this request to prevent this thread from being used to process other requests. If a synchronous 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 allocated to the request is in the suspended state before the return result is called. This affects scalability because the available threads in the thread pool are limited. If all request processing threads are blocked to wait for the I/O operation to complete, other requests are queued to wait for the thread to release. The best case is reduced throughput because requests can be processed after a long wait. The worst case is that the queue is full, and ASP. Net causes subsequent requests to fail due to the 503 "server unavailable" error.

Asynchronous pages provide excellent solutions to problems caused by I/O binding requests. Page processing starts from the thread pool thread, but when an asynchronous I/O operation starts to respond to ASP. NET signals, the thread returns to the thread pool. When this operation is completed, ASP. NET extracts another thread from the thread pool and processes the request. Because threads in the thread pool are used more efficiently, the scalability is improved. Threads pending for I/O completion can now be used to serve other requests. The direct beneficiary is a request that does not take long time for I/O operations and therefore can quickly access the pipeline. Waiting for a long time to enter the pipeline will have a negative impact on the performance of such requests.

There are few documents related to the basic structure of ASP. NET 2.0 Beta 2 asynchronous pages. Let's look forward to the future of asynchronous pages to make up for this deficiency. Remember, this column covers the beta versions of ASP. NET 2.0 and. NET Framework 2.0.

Asynchronous pages in ASP. NET 1.x

ASP. NET 1.XIn essence, asynchronous pages are not supported, but asynchronous pages can be generated through tenacious efforts and unremitting innovation. For more information, seeMsdnMagazineArticle published in June 2003Use threads and build asynchronous handlers in your server-side Web code", The author of this article is Fritz onion.

The trick here is to implement ihttpasynchandler in the Code hiding class of a page to prompt ASP. net by calling ihttpasynchandler. beginprocessrequest is used to process the request, instead of calling the ihttphandler of the page. processrequest method. Then, your beginprocessrequest implementation can start another thread. This thread calls Base. processrequest so that the page enters its normal request processing life cycle (events such as load and render are completed), except for non-threadpool threads. At the same time, beginprocessrequest is returned immediately after a new thread is started, allowing the thread that executes beginprocessrequest to return to the thread pool.

This is the basic idea, but there are still many precautions in the details. You must implement iasyncresult and return it from beginprocessrequest. This usually means creating a manualresetevent object and sending a signal to processrequest when it is returned in the background thread. In addition, you must provide the thread that calls Base. processrequest. Unfortunately, most of the conventional technologies used to move jobs to background threads (including thread. start, threadpool. queueuserworkitem and asynchronous delegation) in ASP. NET applications are counterproductive because they may "steal" threads from the thread pool, or there is a risk of unrestricted thread growth. The correct asynchronous page implementation uses the custom thread pool, but the custom thread pool class is not easy to write (for more information, seeMsdn magazinePublished in February 2005. Net mattersColumn ).

Mainly in ASP. NET 1.XGenerating asynchronous pages is not impossible, but boring. After one or two attempts, you can't help but think of a better way. Currently, this method is ASP. NET 2.0.

Back to Top

Asynchronous pages in ASP. NET 2.0

ASP. NET 2.0 greatly simplifies asynchronous page generation. First, use the @ page command on the page to introduce the async = "true" attribute, as shown below:

In the background, this will notify ASP. NET to implement ihttpasynchandler in this page. Next, you call the new page. addonprerendercompleteasync method in the early stages of the page lifetime (for example, during page_load) to register a begin method and an end method, as shown in the following code:

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

The following operations are interesting. This page goes through its normal life cycle until the prerender event is triggered. ASP. NET then calls the begin method registered using addonprerendercompleteasync. The task of the begin method is to start asynchronous operations such as database queries or web service calls and return immediately. At this time, the thread allocated to the request is returned to the thread pool. In addition, the begin method returns iasyncresult, which allows ASP. NET to determine the completion time of asynchronous operations. At this time, ASP. NET extracts threads from the thread pool and calls the end method. After the end is returned, ASP. NET executes the rest of the page lifecycle, including the rendering stage. Between the begin return and the end call, the request processing thread can freely serve other requests until the end is called and the delay is displayed. Because the 2.0. NET Framework provides multiple asynchronous operations, you do not even need to implement iasyncresult. Otherwise, the framework is implemented for you.

Figure1Provides an example of the Code hiding class in. The response page contains a label control with the ID "output. This page uses the system. net. httpwebrequest class to extract the content of the http://MSDN.microsoft.com. Then, it analyzes the returned HTML and writes all the href target lists it finds to the label control.

Because the HTTP request takes a long time to return, asyncpage. aspx. CS processes it asynchronously. It registers the begin and end methods in page_load, and in the begin method, it calls httpwebrequest. begingetresponse to enable an asynchronous HTTP request. Beginasyncoperation returns iasyncresult returned by begingetresponse to ASP. NET. When the HTTP request is complete, ASP. NET calls endasyncoperation. Endasyncoperation analyzes the content and writes the result to the label control. The result is then rendered and the HTTP response is returned to the browser.

Figure 2 synchronous and asynchronous page processing

Figure 2 shows the differences between ASP. NET 2.0 synchronization and asynchronous pages. When requesting a synchronization page, ASP. NET allocates a thread in the thread pool for the request and executes the page on the thread. If the request stops performing the I/O operation, the thread is suspended until the operation is completed to complete the lifecycle of the page. On the contrary, asynchronous pages are usually executed through the prerender event. Then, call the begin method registered using addonprerendercompleteasync, and then the request processing thread returns the thread pool. Begin starts an asynchronous I/O operation. When this operation is completed, Asp. net extracts another thread from the thread pool, calls the end method, and executes the rest of the lifecycle of the page on the thread.

Figure 3 asynchronous page point displayed in the tracking output

Mark the "Asynchronous vertex" of the page for the begin call ". The trace in Figure 3 accurately shows where the asynchronous point occurs. If yes, you must call addonprerendercompleteasync-that is, not later than the prerender event on this page before the asynchronous vertex.

Back to Top

Asynchronous Data Binding

Generally, ASP. NET pages do not directly request other pages using httpwebrequest, but they usually query the database and bind the results. Therefore, how do you use an asynchronous page to perform asynchronous data binding?Figure4This operation is performed by the code hidden class in.

Asyncdatabind. aspx. CS and asyncpage. aspx. CS use the same addonprerendercompleteasync mode. However, the beginasyncoperation method of asyncdatabind. aspx. CS calls the new method sqlcommand. beginexecutereader (rather than httpwebrequest. begingetresponse) In ADO. NET 2.0 to execute an asynchronous database query. When the call is complete, endasyncoperation calls sqlcommand. endexecutereader to obtain sqldatareader and store it in private fields. In the event handler for the prerendercomplete event (triggered before the asynchronous operation is completed but the page is rendered), asyncdatabind. aspx. CS binds the sqldatareader to the output gridview control. In terms of appearance, this page is similar to a common (synchronous) page that uses the gridview to display the database query results. However, the page is more scalable internally because it does not suspend the thread pool thread to wait for the query to return.

Back to Top

Asynchronously call Web Services

Another I/O-related task that is usually executed by ASP. NET web pages is to call up Web Services. Since Web service calls take a long time to return, the pages that execute them are ideal for asynchronous processing.

Figure5Displays how to generate an asynchronous page for calling up a Web service. It usesFigure1AndFigure4The same addonprerendercompleteasync mechanism in. The begin method of this page starts an asynchronous web service call by calling the asynchronous begin method of the Web Service proxy. The end method of this page caches the reference to the dataset returned by the web method in the private field, and the prerendercomplete handler binds the dataset to the gridview. For reference, the target web method of the call is shown in the following code:

[WebMethod]public DataSet GetTitles (){    string connect = WebConfigurationManager.ConnectionStrings        ["PubsConnectionString"].ConnectionString;    SqlDataAdapter adapter = new SqlDataAdapter        ("SELECT title_id, title, price FROM titles", connect);    DataSet ds = new DataSet();    adapter.Fill(ds);    return ds;}

This is only one method, but not the only one .. Net Framework 2.0 web service proxy supports two asynchronous calls to Web Services. One is. NET Framework 1.XAnd 2.0. The begin and end methods for each method in the Web Service proxy. The other is the new methodasync method and methodcompleted event provided only by the web service agent of. NET Framework 2.0.

If a Web Service has a method named Foo, except for methods named Foo, beginfoo, and endfoo ,. net Framework 2.0 web service proxy also includes methods named fooasync and events named foocompleted. You can call Foo asynchronously by registering the foocompleted event handler and calling fooasync, as shown below:

proxy.FooCompleted += new FooCompletedEventHandler (OnFooCompleted);proxy.FooAsync (...);...void OnFooCompleted (Object source, FooCompletedEventArgs e){    // Called when Foo completes}

When the asynchronous call starts due to the completion of fooasync, The foocompleted event is triggered, resulting in the call of the foocompleted event handler. The delegation that wraps this event handler (foocompletedeventhandler) and the second parameter that is passed to it (foocompletedeventargs) are generated along with the Web Service proxy. You can use foocompletedeventargs. Result to access the return value of foo.

Figure6Demonstrate the code hiding class that uses the methodasync mode to asynchronously call the gettitles method of Web Services. This page is equivalentFigure5. But its internal implementation is quite different. Asyncwsinvoke2.aspx includes a @ page async = "true" command, similar to asyncwsinvoke1.aspx. However, asyncwsinvoke2.aspx. CS does not call addonprerendercompleteasync; it registers a handler for the gettitlescompleted event and calls gettitlesasync on the Web Service proxy. ASP. NET still delays rendering the page until gettitlesasync is complete. Internally, when an asynchronous call starts and completes, it uses an instance of system. Threading. synchronizationcontext (a new class of 2.0) to receive notifications.

Using methodasync instead of addonprerendercompleteasync to Implement Asynchronous pages has two advantages. First, methodasync injects the simulation, culture, and httpcontext. Current into the methodcompleted event handler, while addonprerendercompleteasync does not. Second, if the page performs multiple asynchronous calls and must be delayed until all calls are completed, use addonprerendercompleteasync to generate an iasyncresult that remains stateless before all calls are completed. With methodasync, this operation is not necessary; you only need to place these calls (unlimited number), the ASP. net engine delays this rendering phase until the last call returns.

Back to Top

Asynchronous task

Methodasync is a simple method to call multiple asynchronous web services from an asynchronous page and delay the rendering phase until all calls are completed. But what if you want to perform some asynchronous I/O operations on an asynchronous page, and these operations do not involve Web Services? In this case, an iasyncresult can be generated in turn. Can it be returned to ASP. NET to allow it to know when the last call was completed? Fortunately, the answer is no.

In ASP. NET 2.0, the system. Web. UI. Page class introduces another method to simplify asynchronous operations: registerasynctask. Registerasynctask has four advantages over addonprerendercompleteasync. First, besides the begin and end methods, registerasynctask also allows you to register the timeout method called when the asynchronous operation cannot be completed for a long time. You can set timeout in declarative mode by adding the asynctimeout attribute to the @ page command on this page. Asynctimeout = "5" sets the timeout value to 5 seconds. The second advantage is that you can call registerasynctask multiple times in a request to register several asynchronous operations. Like methodasync, ASP. NET delays rendering the page until all operations are completed. Third, you can use the fourth parameter of registerasynctask to pass the status to the begin method. Finally, registerasynctask injects the end and timeout methods into the simulated, regional, and httpcontext. Current methods. As mentioned above, the end method registered using addonprerendercompleteasync is not applicable.

In other respects, the asynchronous page dependent on registerasynctask is similar to the asynchronous page dependent on addonprerendercompleteasync. It still needs the async = "true" attribute in the @ page instruction (or equivalent programming instruction, which sets the asyncmode attribute of the page to true, it is still executed through the prerender event as usual. At this time, the begin method registered with registerasynctask is called, and the request is further processed until the last operation is completed. For example,Figure7The Code hiding class inFigure1But it uses registertaskasync instead of addonprerendercompleteasync. Note that the timeout handler named timeoutasyncoperation is called if httpwebrequest. begingetrequest cannot be completed for a long time. The corresponding. aspx file includes an asynctimeout attribute that sets the timeout interval to 5 seconds. Note that the fourth parameter passed to registerasynctask (which can be used to transmit data to the begin method) is null.

Registerasynctask allows an asynchronous page to trigger multiple asynchronous calls and delay rendering until all calls are completed. It also works well for a single asynchronous call, and it provides a timeout option that addonprerendercompleteasync does not have. If you generate an asynchronous page with only one asynchronous call, you can use addonprerendercompleteasync or registerasynctask. However, for asynchronous pages with more than two asynchronous calls, registerasynctask greatly simplifies your operations.

Since the timeout value is set per page rather than per call, you may want to know whether the timeout value of a single call can be changed. Is a simple answer. You can programmatically modify the asynctimeout attribute of the page to change the timeout value one by one, but you cannot allocate different timeout values to different calls initialized from the same request.

Back to Top

Wrap it

Now you know the essence of asynchronous pages in ASP. NET 2.0. They are available in the upcoming ASP. net version is very easy to implement, and its architecture allows you to batch process multiple asynchronous I/O operations in a request, and delay the rendering of the page until all operations are completed. With Asynchronous ADO. net and. net Framework. NET page provides a solution for I/O binding requests that are limited by scalability due to full thread pool.

When generating an asynchronous page, note that asynchronous operations from the same thread pool used by ASP. NET should not be started. For example, calling threadpool. queueuserworkitem at the asynchronous page point will be counterproductive because the method comes from the thread pool, resulting in purely obtaining the zero thread used to process the request. On the contrary, calling asynchronous methods built in the framework (for example, Methods httpwebrequest. begingetresponse and sqlcommand. beginexecutereader) is generally considered safe because these methods tend to use ports to implement asynchronous behavior.

Please send questions and comments to JeffWicked@microsoft.com.

Jeff prosiseYesMsdn magazineSenior editors and authors of several books, includingProgramming Microsoft.Net(Microsoft Press, 2002 ). He is alsoWintellectOne of the founders of a software consulting and education company.

 

Related Article

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.