ASP. NET 2.0 runtime analysis

Source: Internet
Author: User

Overview:
This article is based on ASP. NET 2.0Source codeTo help you understand the request processing process and page compilation model in ASP. NET 2.0.

Keywords:
ASP. NET 2.0 runtime, principle, request processing, page compilation, ASP. NET 2.0 HTTP Runtime

Main categories:
System. Web. httpruntime
System. Web. httpapplicationfactory
System. Web. httpapplication
System. Web. Compilation. buildmanager
System. Web. Compilation. applicationbuildprovider
System. Web. Compilation. buildproviderscompiler
System. Web. UI. pagehandlerfactory

Brief request processing flowchart:

Read suggestions:
Use the reflector tool to view the source code of ASP. NET 2.0 and read it.
 
Analysis:
When we open a browser to ASP. when a request is initiated on an Asp.net page of the net 2.0 website, IIS receives the request first on the server side. When IIS sees the request as an Asp.net page, I am very happy because this request does not need to be processed, to ASP. net ISAPI. The ASP. NET prepare process is too tired to complete the task well. ASP. NET isapiwill give another job and change to a new aspnet_wp.exe to handle the job.
The main task of aspnet_wp.exe is to send requests to a series of managed objects called HTTP pipelines. If ASP. NET isapiis used as the sales manager, aspnet_wp.exe is the production manager, and HTTP pipeline is the Production Pipeline. The supervisor will hand over the order (HTTP request) to the httpruntime team's staff processrequest (httpworkerrequest wr). According to the internal division of labor, httpruntime will eventually be produced in the pipeline by processrequestinternal (httpworkerrequest wr, so processrequestinternal (httpworkerrequest wr) is the focus of our analysis.

Processrequestinternal:
1. Create an httpcontext instance.

2. initialize the first request (ensurefirstrequestinit ).
A) In ensurefirstrequestinit, perform initialization by calling system. Web. httpruntime. firstrequestinit. For example, read web. config configuration to runtimeconfig and load all DLL files from the bin directory.

3. Create an httpwriter instance.

4. Call httpapplicationfactory. getapplicationinstance to create an httpapplication instance.
There are three key methods in httpapplicationfactory. getapplicationinstance:
Httpapplicationfactory. _ theapplicationfactory. ensureinited ();
Httpapplicationfactory. _ theapplicationfactory. ensureappstartcalled (context );
Httpapplicationfactory. _ theapplicationfactory. getnormalapplicationinstance (context );

the following three methods are analyzed one by one:
1) httpapplicationfactory. _ theapplicationfactory. ensureinited ();
This method checks whether the httpapplicationfactory is initialized. If not, use httpapplicationfactory. init.
In Init (), obtain the complete path of the global. asax file, and call compileapplication () to compile global. asax.
How is compilation performed?
the compilation is completed by buildmanager. Buildmanager first obtains globalasaxtype (that is, httpapplication), and then calls buildmanager. getglobalasaxbuildresult () = "getglobalasaxbuildresultinternal () =" ensuretoplevelfilescompiled () for compilation.
compile compilationstage. toplevelfilescompiled to compile the files in the following three directories:
A. compileresourcesdirectory ();
compile the app_globalresources directory.
B. compilewebrefdirectory ();
compile the app_webreferences directory.
C. compilecodedirectories ();
compile the app_code directory.

compile compilationstage. globalasax to compile global. asax. method call: compileglobalasax () = applicationbuildprovider. getglobalasaxbuildresult (buildmanager. isprecompiledapp ).
the specific compilation in getglobalasaxbuildresult is completed by applicationbuildprovider and buildproviderscompiler.
buildproviderscompiler. Compile mbuild.
applicationbuildprovider. getbuildresult.
after compilation, a DLL file similar to app_global.asax.mlgx7n2v.dll is generated in the c: \ windows \ Microsoft. NET \ framework \ v2.0.50727 \ temporary ASP. NET files \ directory.
the compiled class named ASP. global_asax inherits from httpapplication.
Note: if the global. asax file does not exist in the web directory, files such as app_global.asax.mlgx7n2v.dll will not be compiled.

2) httpapplicationfactory. _ theapplicationfactory. ensureappstartcalled (context );
Create a specific httpapplication instance, trigger the applicationonstart event, and execute the application_start (Object sender, eventargs e) method in ASP. global_asax. The httpapplication instance created here is recycled after the event is processed.

3) httpapplicationfactory. _ theapplicationfactory. getnormalapplicationinstance (context );
This method creates an httpapplication instance and initializes it (call the system. Web. httpapplication. initinternal () method ).
Httpapplication instances are created based on the actual _ theapplicationtype. If the web directory does not contain the global. Asa file, that is, the ASP. global_asax type is not dynamically compiled, the httpapplication is directly instantiated. If the ASP. global_asax type is created, the ASP. global_asa is instantiated.

After an httpapplication instance is created, the initinternal method of the instance is called.
The initinternal method is also our key analysis method. The main functions of this method are as follows:
1. initmodules (): Create the corresponding httpmodules according to the settings of Web. config.

2. hookupeventhandlersforappplicationandmodules: calls the corresponding event processing function in the httpapplication instance based on the event.

3. create many instances of classes that implement the iexecutionstep interface and add them to _ execsteps of the current httpapplication instance, waiting for the callback to be executed. From this we can see that httpapplication processes Requests asynchronously, and many of the request processing work is put into _ execstep for execution while waiting for the callback.
_ Execstep:
1) perform a security check on the Request Path and prohibit unauthorized path access (validatepathexecutionstep ).
2) If urlmappings is set, perform rewritepath (urlmappingsexecutionstep ).
3) execute event processing functions, such as beginrequest and authenticaterequest.
4) obtain the httphandler that processes the current request. dynamic compilation of ASP. NET pages is also carried out here. (Maphandlerexecutionstep)
This process is done by calling the system. Web. httpapplication. maphttphandler method.
In maphttphandler, first obtain the corresponding implementation ihttphandlerfactory type from web. config Based on the access address. For Asp.net pages, the default value is pagehanlderfactory. Create a pagehanlderfactory instance, call gethandlerhelper, and call buildmanager. createinstancefromvirtualpath in gethandlerhelper to compile and create an ASP. NET page instance of the current request (if it has been compiled, load it directly from the cache ).
Createinstancefromvirtualpath after several method calls, the compilation task is assigned to buildmanager. compilewebfile (). Compilewebfile obtains the corresponding buildprovider from web. config. For the. aspx file, the corresponding buildprovider is pagebuildprovider. How does pagebuildprovider compile pages? Further analysis is not performed here. If you are interested, you can further study the source of ASP. NET 2.0. Code .
5) Call The. processrequest method of the corresponding httphandler to process the request (if asynchronous, call beginprocessreques ). (Callhandlerexecutionstep)
6) write the response content to the filter. (Callfilterexecutionstep)

5. Call the beginprocessrequest of the httpapplication instance to process requests asynchronously.
Many of the things in _ execsteps mentioned above are executed after httpruntime calls httpapplication beginprocessrequest and calls resumesteps in beginprocessrequest.

Asp. NET 2.0 Runtime is ASP. NET 2.0 is very complex and hard to understand. It is also very important for ASP. NET 2.0 runtime source code research is in our deepening ASP. NET 2.0 principle understanding, will give us the development of ASP. NET 2.0 application Program brings a lot of help. This article is my first time learning ASP. NET 2.0 runtime, to help you better understand ASP. NET 2.0 is sometimes written during running. You are welcome to criticize and suggest the content of Article .
I think writing an article not only improves my writing skills, but also facilitates communication, you can clarify your own ideas, promote your in-depth thinking, and deepen your understanding of the technology. It is helpful for developers to write some technical articles after coding.

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.