asp.net operation principle detailed _ practical skill

Source: Internet
Author: User
Main class:
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

Request Processing Brief Flowchart:


Read suggestions:

Use the Reflector tool to view the ASP.net 2.0 source code while reading.

Analysis:

When we made a request through the browser to a asp.net page of the ASP.net 2.0 website, IIS received the request first on the server side, and it was very happy to see the ASP.net page, because this request does not need it to handle, to ASP.net ISAPI on the line. ASP.net ISAPI is also easier to do, and his main task is to schedule aspnet_wp.exe to process requests and monitor the execution of aspnet_wp.exe processes if aspnet_ Wp.exe process is too tired, not outstanding to complete the task, ASP.net ISAPI will let him laid-off, change a new aspnet_wp.exe to deal with work.

The primary task of Aspnet_wp.exe is to deliver the request to a series of managed objects called HTTP pipes. If the ASP.net ISAPI than the sales manager, that aspnet_wp.exe is the production Manager, and the HTTP pipeline is the production line. The team responsible for the assembly line is httpruntime, and production manager aspnet_wp.exe will hand over the order (HTTP request) to the HttpRuntime team's staff ProcessRequest (HttpWorkerRequest wr), HttpRuntime according to the internal division of labor, ultimately by ProcessRequestInternal (HttpWorkerRequest WR) in the production line, so ProcessRequestInternal ( HttpWorkerRequest WR) is the focus of our analysis.

the main tasks of processrequestinternal are:

1. Create a HttpContext instance.

2. Initialization of the first request (Ensurefirstrequestinit).

A In Ensurefirstrequestinit, some initialization work is done by calling System.Web.HttpRuntime.FirstRequestInit, such as reading web.config configuration to Runtimeconfig, loading from the bin directory There is a DLL file.

3. Create a Httpwriter instance.

4. Create HttpApplication instance by calling Httpapplicationfactory.getapplicationinstance.

There are three key methods in Httpapplicationfactory.getapplicationinstance:

Httpapplicationfactory._theapplicationfactory.ensureinited ();
httpapplicationfactory._theapplicationfactory.ensureappstartcalled (context);
Httpapplicationfactory._theapplicationfactory.getnormalapplicationinstance (context);

Here we analyze each of these three methods individually:

1) httpapplicationfactory._theapplicationfactory.ensureinited ();

The method checks whether the httpapplicationfactory is initialized, and if not, initializes it by Httpapplicationfactory.init ().
In Init (), first get the full path to the Global.asax file, and then call CompileApplication () to compile the global.asax.
So how did the compilation work?

The compiled work is done by BuildManager. BuildManager first get Globalasaxtype (i.e. HttpApplication), and then call Buildmanager.getglobalasaxbuildresult () = " Getglobalasaxbuildresultinternal () = "ensuretoplevelfilescompiled ()" is compiled.

In ensuretoplevelfilescompiled, first compile the compilationstage.toplevelfiles 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 ();

Compiles the App_Code directory.

Then proceed to Compilationstage.globalasax compile, compile to Global.asax, method call Condition: compileglobalasax () = " Applicationbuildprovider.getglobalasaxbuildresult (Buildmanager.isprecompiledapp).

In Getglobalasaxbuildresult, the concrete compilation is done by Applicationbuildprovider and Buildproviderscompiler together.

Buildproviderscompiler.performbuild (), compile work.

Applicationbuildprovider.getbuildresult gets the result of compiling.

After the compilation succeeds, a similar app_ is generated in the C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary asp.net files\ corresponding directory A global.asax.mlgx7n2v.dll DLL file.

The compiled generated class name is Asp.global_asax and inherits from HttpApplication.

Note: If there is no Global.asax file in the Web directory, the build App_global.asax.mlgx7n2v.dll file will not be compiled.

2) httpapplicationfactory._theapplicationfactory.ensureappstartcalled (context);

Creates a specific HttpApplication instance, triggers the Applicationonstart event, and executes the Application_Start (object sender, EventArgs e) method in Asp.global_asax. The HttpApplication instance created here is reclaimed after the event has been processed.

3) httpapplicationfactory._theapplicationfactory.getnormalapplicationinstance (context);

This method creates a HttpApplication instance and initializes it (calls System.Web.HttpApplication. Initinternal () method).
The HttpApplication instance is created based on the actual _theapplicationtype. If there is no Global.asa file in the Web directory, that is, no dynamic compilation generates the Asp.global_asax type, then the HttpApplication is instantiated directly. If the Asp.global_asax type is created, the Asp.global_asa is instantiated.

  after the HttpApplication instance is created, it is the Initinternal method that invokes the instance.

Initinternal method is also our key analysis method, the main function of this method is as follows:

1. Initmodules (): According to the Web.config settings, create the corresponding httpmodules.

2. Hookupeventhandlersforappplicationandmodules: invokes the corresponding event handler in the HttpApplication instance according to the occurrence of the event.

3. Create many instances of the classes that implement the IExecutionStep interface and add them to the _execsteps of the current HttpApplication instance, waiting for the callback to execute. From here we can see that HttpApplication is processing the request asynchronously, and that much of the processing of the request is placed in _execstep waiting for the callback to execute.

The main processing work in _execstep is as follows:

1 Check the path of the request, prohibit illegal path access (validatepathexecutionstep).

2 If the urlmappings is set, proceed to RewritePath (Urlmappingsexecutionstep).

3 perform event handler functions, such as BeginRequest, AuthenticateRequest, and so on.

4 Gets the httphandler,asp that handles the current request. The Run-time compilation of the net page is also done here. (Maphandlerexecutionstep)
The processing is done by invoking the System.Web.HttpApplication. Maphttphandler method.

In Maphttphandler, the corresponding type of implementation ihttphandlerfactory is first obtained from web.config based on the address of the access. For asp.net pages, the default is Pagehanlderfactory. Then create the Pagehanlderfactory instance, call Gethandlerhelper, Call Buildmanager.createinstancefromvirtualpath in Gethandlerhelper to compile and create an instance of the ASP.net page of the current request (loaded directly from the cache if it has been compiled).
Createinstancefromvirtualpath the compilation task to BuildManager after several method calls. Compilewebfile (). Compilewebfile gets the corresponding buildprovider from the web.config, and for the. aspx file, the corresponding BuildProvider is Pagebuildprovider. Pagebuildprovider How to compile the page, there is no further analysis, if you are interested, you can further study the source code of ASP.net 2.0.

5) call the corresponding HttpHandler. ProcessRequest method to process the request (if it is asynchronous, call Beginprocessreques). (CallHandlerExecutionStep)

6 Write the response content to filter. (Callfilterexecutionstep)

5. Invoke the BeginProcessRequest asynchronous processing request of the HttpApplication instance.

Many of the things that happen in the _execsteps above are called HttpApplication beginprocessrequest after httpruntime. Executed after the resumesteps is invoked in BeginProcessRequest.

asp.net 2.0 runtime is asp.net 2.0 is very complex, difficult to understand is also a very important part of the ASP.net 2.0 run-time source code research is in our deepened understanding of the ASP.net 2.0 principles, will give us the development of ASP.net 2.0 applications bring a lot of help. This article is my first time to learn asp.net 2.0 run, in order to help myself better understand ASP.net 2.0 run, write, you are welcome to the content of the article to put forward criticism and suggestions.

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.