ASP.net 2.0 page Performance considerations

Source: Internet
Author: User
Tags foreach net thread thread tostring web services
Asp.net| Performance | Page In general, we usually in a page, combined with the page event processing model, step-by-step in order to declare 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 (Beginasyncoperation),
New EndEventHandler (Endasyncoperation)
);
}

IAsyncResult Beginasyncoperation (object sender, EventArgs E, AsyncCallback cb, Object State)
{
M_request = WebRequest.Create ("http://www.dofor.cn");
Return M_request.begingetresponse (CB, State);
}

void Endasyncoperation (IAsyncResult ar)
{
string text;
using (WebResponse response = M_request.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 ();
}
}
The second is to register the asynchronous task (register asynchronous task). RegisterAsyncTask has greater flexibility and advantages over AddOnPreRenderCompleteAsync. It can allow you to declare a timeout parameter, as well as to declare in @page such as: asynctimeout= "5", in seconds, but note that this declaration is not a timeout for each asynchronous process, but a processing time for the entire page. The. NET Framework 2.0 also introduces new Methodasync,methodasync for registration tasks to facilitate the processing of multiple asynchronous processes. 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 AsyncPageTask:System.Web.UI.Page
{
Private WebRequest m_request;

protected void Page_Load (object sender, EventArgs e)
{
PageAsyncTask task = new PageAsyncTask (
New BeginEventHandler (Beginasyncoperation),
New EndEventHandler (Endasyncoperation),
New EndEventHandler (Timeoutasyncoperation),
Null
);

RegisterAsyncTask (Task);
}

IAsyncResult Beginasyncoperation (object sender, EventArgs E, AsyncCallback cb, Object State)
{
M_request= webrequest.create ("http://www.dofor.cn");
Return M_request.begingetresponse (CB, State);
}

void Endasyncoperation (IAsyncResult ar)
{
string text;
using (WebResponse response = M_request.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 ();
}

void Timeoutasyncoperation (IAsyncResult ar)
{
Output.text = "Current data is not available";
}
}
In short, reasonable use of ASP.net 2.0 asynchronous processing, you can greatly improve the performance of the high throughput of concurrent web pages.

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.