In-depth analysis of the implementation mechanism of httpmodule from the request pipeline.

Source: Internet
Author: User

To understand the underlying principles, you must have a little understanding of the request processing process and page lifecycle to learn more:

The request processing process and page lifecycle will be explained in depth in the following days.

The implementation mechanism of httpmodule is as follows:

1. When a request arrives at isapiruntime, use the processreqeust (hereinafter referred to as Pr) method to create an httpwrokrequest object.

2. When executing the isapiruntime PR method, the httpruntime PR method inside the method creates the context object httpcontext Based on the httpworkrequest object.

3. In the httpruntime PR method, an httpapplication instance for processing the application is created through httpapplicationfactory.

Note: httpapplication is created according to global. the type of the asax file after compilation, and then the instance created through the reflection method, because the instance creation process consumes a lot of time and resources, this uses the object pool technology

4. During the process of creating an httpapplication instance, The initinternal method is called internally. In this method, the initialization method of httpmodule is called to implement event registration.

Note: During event registration, check whether the httpmodule module is configured in the configuration file. If yes, use reflection registration. If not, continue until the method jumps out. This process is the embodiment of Microsoft's plug-in mechanism.

5. After the event is registered, the httpapplication instance starts to call its own PR method to start executing the page lifecycle.

Conclusion: The event registration of the httpmodule module is to call the initinternal method within the httpapplication instance. The initinternal method calls the initialization method of the httpmodule to implement event registration.

 

The following code helps you understand the process:

1. First define a context class httpcontext

 

1 using system; 2 using system. collections. generic; 3 using system. LINQ; 4 using system. text; 5 6 namespace httpapplicationprocessmethoddemo 7 {8 // <summary> 9 // Context 10 /// </Summary> 11 public class httpcontext12 {13} 14}

 

2. Define two interfaces: ihttphandler and ihttpmodule

Using system; using system. collections. generic; using system. LINQ; using system. text; namespace httpapplicationprocessmethoddemo {/// <summary> /// interface, this interface is mainly used to call the PR method of a specific page or general handler when the application calls the PR method. /// </Summary> Public interface ihttphandler {void processrequest (httpcontext context );}}
Using system; using system. collections. generic; using system. LINQ; using system. text; namespace httpapplicationprocessmethoddemo {/// <summary> /// interface, this interface is used to simulate event registration in the initinternal method of the application. // </Summary> Public interface ihttpmodule {void Init (httpapplication application );}}

3. Define a page class page

Using system; using system. collections. generic; using system. LINQ; using system. text; namespace httpapplicationprocessmethoddemo {// <summary> /// page class /// </Summary> public class page: ihttphandler {// <summary> // implements the ihttphandler interface // </Summary> /// <Param name = "context"> context </param> Public void processrequest (httpcontext context) {console. writeline ("Page lifecycle .... "); console. writeline (".................. "); console. writeline (".................. "); console. writeline (".................. "); console. writeline ("the lifecycle of the page ends... ");}}}

4. Define an application Class Application

Using system; using system. collections. generic; using system. LINQ; using system. text; namespace httpapplicationprocessmethoddemo {public class httpapplication: ihttphandler {// initialization method public void initinternal () {// read all the Assembly registered with httpmodule from the configuration file, then, reflect the instance and call the init Method !!! The following myhttpmodule assumes that ihttpmodule httpmodule = new myhttpmodule (); httpmodule. init (this); bindevent () ;}// application's own event response method private void bindevent () {beginrequest ++ = new eventhandler (httpapplication_beginrequest ); postresolverequestcache + = new eventhandler (httpapplication_postresolverequestcache); endrequest + = new eventhandler (httpapplication_endrequest);} void httpapplication_endrequest (Object sender, eventargs e) {Console. writeline ("application's event response method executed -- endrequest");} void httpapplication_postresolverequestcache (Object sender, eventargs e) {console. writeline ("application executes the -- postresolverequest");} void httpapplication_beginrequest (Object sender, eventargs e) {console. writeline ("application's event response method executed -- beginrequest");} // This method is considered as the public void processrequest (httpcontext context) pipeline for HTTP request processing) {// 19 events, 23 steps console. writeline ("START request"); // triggers the first event beginrequest (this, null); // triggers the seventh event postresolverequestcache (this, null); console. writeline ("cache obtained"); // between the seventh and eighth events, create a page object or a general handler ihttphandler httphandler = new page (); console. writeline ("create Page Object"); // execute the PR method console between 11 and 12 events. writeline ("start execution page lifecycle"); httphandler. processrequest (context); // the last event endrequest (this, null); console. writeline ("end request");} public event eventhandler beginrequest; public event eventhandler postresolverequestcache; public event eventhandler endrequest ;}}

5. Simulate the execution process in the request Pipeline

Using system; using system. collections. generic; using system. LINQ; using system. text; namespace httpapplicationprocessmethoddemo {class program {static void main (string [] ARGs) {// isapiruntime // assume that the httpcontext context = new httpcontext () is created based on the request (); // assume that httpapplication application = new httpapplication () is created from httpapplicationfactory; // load all the httpmodules registered in the configuration file and execute the init method application. initinternal (); // call the PR method to start executing the PR Method Application on the page. processrequest (context); console. readkey ();}}}

6. customize an httpmodule

Using system; using system. collections. generic; using system. LINQ; using system. text; namespace httpapplicationprocessmethoddemo {// <summary> /// custom httpmodule // </Summary> public class myhttpmodule: ihttpmodule {// <summary> /// implements the ihttpmodule interface // </Summary> // <Param name = "application"> </param> Public void init (httpapplication Application) {// register the event application. beginrequest + = new eventhandler (application_beginrequest); application. postresolverequestcache + = new eventhandler (application_postresolverequestcache); application. endrequest + = new eventhandler (application_endrequest);} void application_endrequest (Object sender, eventargs e) {console. writeline ("httpmodule registered endrequest method");} void application_postresolverequestcache (Object sender, eventargs e) {console. writeline ("httpmodule registered postresolverequestcache method");} void application_beginrequest (Object sender, eventargs e) {console. writeline ("httpmodule registered beginrequest method ");}}}

Through the above steps, the execution process of the entire pipeline and the implementation principle of httpmodule are realized.

Below is:

 

 

 

 

 

 

 

 

 

 

 

 

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.