ASP. NET large file upload topic (2)-page generation process

Source: Internet
Author: User

Review the previous article to learn about the following:

1. By default, only files smaller than 4 MB can be uploaded. If you want to upload large files, you can change the maxrequestlength to raise the limit.
2.asp.net 1.x you can increase the upload limit by changing the maxrequestlength. However, the server performance is greatly affected because the entity content requested by the user needs to be fully loaded into the memory before processing.
3.asp.net 2.0 is transparently buffered to the disk after the object content requested by the user exceeds a certain threshold or limit value (256 K. in. NET 2.0, the server's memory will not be exhausted due to abnormal client requests.

Summary:

This article mainly clarifies the process of HTTP request flow from arriving at the web server to generating the page. In order to better understand the content in this article, we recommend that you first take a look at the "http request process" and "ASP. NET application lifecycle ";

Body content:

Some friends may be impatient. "What is the relationship between this and uploading large files ?". That's because we cannot pass. NET provides the upload Control for us to get the desired effect. If we want to upload large files and display the progress, only after the server receives the HTTP request, process the request. Then we need to have an understanding of the HTTP request process on the server side.
After the browser sends a page request (including get, post, put, and other request methods) to the IIS serverProgramAfter receiving, only a few types of resources requested by the client are directly processed by IIS. For example, IIS processes incoming requests to HTML pages, text files, JPEG and GIF images. Requests to the Active Server Page (*. asp) file are parsed by calling the ASP extension module named ASP. dll. Similarly, requests for ASP. NET Resources (for example, *. aspx, *. asmx, and *. ashx) are passed to the ASP. net isapi extension. Because IIS 6.0 is changed in IIS 5.x, we separate it.

1. First, let's look at the Asp.net request processing process of IIS 5.x:

It can be seen that the IIS 5.x detection program is implemented by the iisprocess (inetinfo.exe). In addition to the HTTP message reception function, it also directly embeds aspnet_isapi.dll (Asp.net ISAPI extension) in the process. After IIS receives the message, it checks the script ing and then calls ASP.. Net ISAPI expansion, and the expansion transfers requests and control to all relevant information to the auxiliary process aspnet_wp.exe (this auxiliary process is also called by Asp.net ISAPI and is automatically loaded when the process is initialized.. Net runtime. net runtime. Because ASP. net isapi extensions and. Net runtime do not belong to one process, all request data is sent through the named pipeline.

2. Next let's take a look at the Asp.net request processing process of IIS 6.0:

As shown in the figure, inetinfo.exe is no longer used to transmit HTTP requests to ISAPI extensions in IIS 6.0. However, it continues to provide services for requests of other protocols, instead of HTTP. sys's Windows kernel-mode device driver is implemented. It does not process the requests it receives but transmits the HTTP requests it receives to the user model that is running the website (this is not the aspnet_wp.exe, but the executable file named w3wp.exe ), the corresponding ISAPI and. net runtime, so that the ISAPI module and. net runtime environment data communication, thus avoiding the loss caused by inter-process communication.

So what happens when an HTTP request is extended by ASP. net isapi to the. NET runtime?

The isapiruntime instance first calls the processrequest method. This method receives a ECB (an unmanaged object that contains all the underlying request information such as server variables, HTTP input streams, and HTTP output streams) and a server type parameter iwrtype (this parameter is used to specify the version of isapiworkerrequest to be created), and then it is passed to the isapiworkerrequest object, which is responsible for creating an httpworkerrequest object that represents the current request. Isapiworkerrequest is an abstract class inherited from httpworkerrequest. For different IIS versions, it has different isapiworkerrequest subclasses, such as IIS 5.x, isapiworkerrequestinprocforiis6, and IIS. Processrequest uses the iwrtype passed in by ISAPI to create different httpworkerrequests, thus shielding the differences between different IIS, so that subsequent operations do not need to consider this difference.

Public   Int Processrequest (intptr ECB, Int Iwrtype)
{
Int Num;
Try
{
Httpworkerrequest WR = Isapiworkerrequest. createworkerrequest (ECB, iwrtype );
String Apppathtranslated = Wr. getapppathtranslated ();
String Appdomainapppathinternal = Httpruntime. appdomainapppathinternal;
If (Appdomainapppathinternal =   Null ) | Stringutil. equalsignorecase (apppathtranslated, appdomainapppathinternal ))
{ // at the beginning, the request processing process is handed over to httpruntime
httpruntime. processrequestnodemand (WR);
// All HTTP requests in the processrequestnodemand method are queued and executed sequentially, and processrequestinternal (httpworkerrequest wr) is triggered) method
return 0 ;
}
Httpruntime. shutdownappdomain (applicationshutdownreason. physicalapplicationpathchanged, Sr. getstring ( " Hosting_phys_path_changed " , New   Object [] {Appdomainapppathinternal, apppathtranslated} ));
Num =   1 ;
}
Catch (Exception)
{
Misc. reportunhandledexception (exception, New   String [] {Sr. getstring ("Failed_to_process_request")} );
Throw ;
}
Return Num;
}

From the aboveCodeWe can see that the processrequestinternal method is called in the end. This method is very important. Let's take a look at what it mainly does.

 

Processrequestinternal
Private   Void Processrequestinternal (httpworkerrequest wr)
{
Httpcontext extradata =   New Httpcontext (WR, False );
Wr. setendofsendnotification ( This . _ Asyncendofsendcallback, extradata );
Interlocked. increment ( Ref   This . _ Activerequestcount );
Hostingenvironment. incrementbusycount ();
Try
{
Try
{
This . Ensurefirstrequestinit (extradata );
}
Catch
{
If ( ! Extradata. Request. isdebuggingrequest)
{
Throw ;
}
}
Extradata. response. initresponsewriter ();
Ihttphandler applicationinstance = Httpapplicationfactory. getapplicationinstance (extradata );
If (Applicationinstance =   Null )
{
Throw   New Httpexception (Sr. getstring ( " Unable_create_app_object " ));
}
If (Etwtrace. istraceenabled ( 5 , 1 ))
{
Etwtrace. Trace (etwtracetype. etw_type_start_handler, extradata. workerrequest, applicationinstance. GetType (). fullname, " Start " );
}
If (Applicationinstance Is Ihttpasynchandler)
{
Ihttpasynchandler handler2 = (Ihttpasynchandler) applicationinstance;
Extradata. asyncapphandler = Handler2;
Handler2.beginprocessrequest (extradata, This . _ Handlercompletioncallback, extradata );
}
Else
{
Applicationinstance. processrequest (extradata );
This . Finishrequest (extradata. workerrequest, extradata, Null );
}
}
Catch (Exception)
{
Extradata. response. initresponsewriter ();
This . Finishrequest (WR, extradata, exception );
}
}

 

1. Check whether the current httpruntime instance is called for the first time. If it is called for the first time, create and initialize the core object httpcontext through the firstrequestinit function.
The httpcontext class contains the Objects requested by the current application, such as httprequest and httpresponse objects. The httprequest object contains information about the current request, including cookie and browser information. The httpresponse object contains the responses sent to the client, including all rendered outputs and cookies.
2. Call the httpresponse. initresponsewriter function to initialize the returned object httpworkerrequest. Response of the page request.

3. Call httpapplicationfactory. getapplicationinstance to create an instance of the httpapplication class and call the initinternal method of the httpapplication instance to start the application.

After initinternal is called, The httpapplication instance first initializes the web. all the modules registered in config (httpmodule event processor, which should be especially remembered, because if we really start to write code, it is mainly written in this module ), call the init method of the httpapplication class. The following events are triggered in sequence.

1. Initiate a beginrequest event, which is always the first event that occurs during request processing.
2. Trigger the authenticaterequest event.
3. Trigger the postauthenticaterequest event.
4. Cause the authorizerequest event.
5. Trigger the postauthorizerequest event.
6. Trigger the resolverequestcache event.
7. The postresolverequestcache event is triggered.
8. Based on the file extension of the requested resource (ing in the application configuration file), select the class that implements ihttphandler to process the request. If the request is for an object (PAGE) derived from the page class and needs to compile the page, ASP. NET will compile the page before creating an instance.
9. Trigger the postmaprequesthandler event.
10. The acquirerequeststate event is triggered.
11. Trigger the postacquirerequeststate event.
12. Trigger the prerequesthandlerexecute event.
13. Call the appropriate Ihttphandler Class processrequest method (or asynchronous beginprocessrequest ). For example, if the request is for a page, the current page instance processes the request.
14. The postrequesthandlerexecute event is thrown. 15. Trigger the releaserequeststate event.
16. The postreleaserequeststate event is triggered.
17. If the filter attribute is defined, the response is filtered.
18. The updaterequestcache event is triggered.
19. Trigger the postupdaterequestcache event.
20. An endrequest event is triggered.

From the above 8th events, we can see that the page is compiled in this step and an instance of the ASP. NET page requested before it is created (if it has been compiled, it is loaded directly from the cache ).

Finally, let's review:

 

1. First, inetinfo.exe receives the HTTP request

2. Check the script ing and then call ASP. net isapi extension.

3.send the message to the aspnet_wp.exe process and load the runtime

4. Call the isapiruntime. processrequest method to create an httpworkerrequest object

6. Create an httpcontext request Context

7. Create an instance of the httpapplication class

8. Initialize all modules registered in Web. config (httpmodule)

9. Call the init method of the httpapplication class

10. Trigger the event and instantiate the page.

This article is written due to insufficient knowledge.ArticleIt took a long time, but I also learned deeply about. Net runtime. The article may not be well written, because the information you are looking for has some small discrepancies in some content, so you are welcome to give your comments,

Next article

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.