When requesting a common time-consuming Asp.net web page (slow database query, requesting a network resource, etc ), asp.net extracts an available thread from its own thread pool to process web pages (including Object Instantiation, corresponding event processing, HTML Rendering, etc ), when there are no available threads in the thread pool (the site request frequency is very high, and all threads in the thread pool are processing requests), new requests will be pushed into a queue. If the queue is full, asp.net returns the 503 error of "server unavailable" to reject new requests.
The number of threads in the thread pool and the queue size are affected by multiple factors, including the IIS version,. NET Framework version, and the number of machine CPUs.
When a slow database query or a network resource is requested to return results, the currently used threads are suspended. These threads neither process other logic nor occupy the CPU, however, the throughput of the entire website is seriously affected because the site should handle more requests.
In this case, you can use Asp.net asynchronous pages to increase the throughput of the site, and use Asp.net asynchronous pages to process time-consumingCodeTransfer to a non-Asp.net thread. When asynchronous results are returned, Asp.net will be notified and an available thread will be taken from the Asp.net thread pool to process the end code and render the HTML.
note the following when using Asp.net asynchronous pages:
1. asynchronous thread usage
try to use the built-in asynchronous support of framework, sqlcommand, httpwebrequest, filestream and other objects to provide the related beginxx () and endxx () methods, the asynchronous methods of these objects are not the threads in the Asp.net thread pool.
use the delegate begininvoke () or threadpool. queueuserworkitem () is a thread that can be retrieved from the Asp.net thread pool. Using them makes no sense to solve the problem. creating a custom processing thread through the tread class is dangerous if the created thread cannot be properly managed (if there are n requests at the same time, there will be n threads created, too many creation threads will affect the performance of Asp.net and may cause the Asp.net site to be unable to process requests normally ).
2. asynchronous pages are not faster than normal pages
asynchronous pages are not faster than normal pages because of the thread switching process. of course, you can use registerasyctask () to execute multiple concurrent tasks on the Asp.net asynchronous page.
3. error Handling
when using an asynchronous page, you must handle the exception. If the exception is not handled properly, the entire process may be indicated. Exceptions occurring in an asynchronous operation will be called in endxx () method.
if an exception occurs before the iasyncresult object is returned, you can return a custom object that implements the iasyncresult interface.
for example, execute sqlconnection before calling the beginexecutenonquery (asynccallback callback, object stateobject) method of the sqlcommand class.
. an exception occurs when the open () method is used. Because the asynchronous operation is not started when the database connection is opened, the exception here is not thrown when
the endexecutereader (iasyncresult asyncresult) method is called, therefore, a custom iasyncresult object must be returned when an exception occurs during database connection capturing.