ASP.net 2.0 page Performance considerations

Source: Internet
Author: User
Tags net thread thread web services

Generally we usually in a page, combined with the page event processing model, in order step-by-Step declaration of each process. This is called synchronous processing (synchronous), we can easily define the order of page processing. While it is convenient to synchronize, it exposes serious problems in time-consuming request processing, and on a highly concurrent web site production server, Because. NET processing is based on so-called threadpool, and the processing threads in ThreadPool are limited, if the threads in the current threadpool are fully exhausted, and then there is a continuous mass request, the performance of the server will be severely degraded and the server will crash even more seriously. So the most we care about is how to better use the. NET ThreadPool. Allow. NET to better perform its performance.

Speaking of which, we have to talk about. NET asynchronous processing (asynchronous), the model typically returns a Iasycresult object for beginxxx,endxxx,beginxxx that contains information about the current asynchronous operation, and the ENDXXX user accepts the return value. Output parameters.. NET allocates an idle thread from the ThreadPool to BeginXxx and then returns immediately to ThreadPool, and when asynchronous processing ends,. NET also assigns an idle thread from the ThreadPool to handle the EndXxx method. This may have enough threads to handle other things. In the space question here we only talk about the asynchronous processing model of ASP.net 2.0, we all know that in the 1.x time to create a good page of asynchronous processing is more cumbersome, perhaps Microsoft recognized this early, so in the 2.0 version of the new page processing model, added a convenient asynchronous processing point (asynchronous point) between the PreRender event and the Prerendercomplete event, at which the page needs to wait for all asynchronous processing to complete, so you can accomplish whatever effect you need before the page is rendered, Greatly simplifies the method of establishing an asynchronous page.

First you have to add async= "true" to the @page declaration that creates the page, which is necessary. The settings tell asp.net to select IHttpAsyncHandler to process the current page. The next thing you need to do is to use the asynchronous process in Page_Load event processing, and now there are two ways:

The first is to use the Page.addonprerendercompleteasync method to handle the set of begin and end methods that need to be processed asynchronously.

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

By adding the above method, the page executes the usual lifecycle event until the PreRender event of the page is triggered. Then asp.net calls the begin handler that was previously registered in AddOnPreRenderCompleteAsync. Typically, in the begin handler, some asynchronous Web services, IO, and SQL processing are handled, which can greatly ease the pressure on the. NET thread pool. For example:

<%@ Page async= "true" language= "C #"%>
<asp:content id= "Content" contentplaceholderid= "Main" runat= "Server"
<asp:label id= "Output" runat= "Server" ></ASP:LABEL>
</asp:content>
  public partial class AsyncPage:System.Web.UI.Page
{
Private WebRequest m_request;
void Page_Load (object sender, EventArgs e)
{
AddOnPreRenderCompleteAsync (
New BeginEventHandler (beginasy   ncoperation),
New EndEventHandler (endasyncoperation)
;

IAsyncResult beginasyncoperation (object sender, EventArgs E, AsyncCallback cb, object state)
{
M_reques t = webrequest.create ("http://www.dofor.cn");
Return M_request.begingetresponse (CB, State);
}
void Endasyncoperation (IAsyncResult ar)
{
string text;
using (WebResponse response = M_reque St. EndGetResponse (AR))
{
using (StreamReader reader = new StreamReader (response). GetResponseStream ())
{
Text = ReadEr. ReadToEnd ();
}
}
Regex regex = new Regex ("Href\\s*=\\s*\" ([^\ "]*) \" ", regexoptions.ignorecase);
MatchCollection matches = regex. Matches (text);
StringBuilder builder = new StringBuilder (1024);
foreach (match match in matches)
{
Builder. Append (match. GROUPS[1]);
Builder. Append ("<br/>");
}
Output.text = Builder. ToString ();
}
}

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.