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 this process:
1. First define a context class HttpContext
Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace HttpApplicationProcessMethodDemo
{
/// <Summary>
/// Context
/// </Summary>
Public class HttpContext
{
}
}

2. Define two interfaces: IHttpHandler and IHttpModule
Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace HttpApplicationProcessMethodDemo
{
/// <Summary>
/// Interface. This interface is mainly used when the application calls the pr method.
/// Call the pr method of a specific page or general handler.
/// </Summary>
Public interface IHttpHandler
{
Void ProcessRequest (HttpContext context );
}
}

Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace HttpApplicationProcessMethodDemo
{
/// <Summary>
/// Interface, which is used to simulate event registration within the InitInternal method of the Application
/// </Summary>
Public interface IHttpModule
{
Void Init (HttpApplication application );
}
}

3. Define a Page class Page
Copy codeThe Code is as follows:
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
Copy codeThe Code is as follows:
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 Assemblies registered with HttpModule from the configuration file, then reflect the instance, and call the Init Method !!! The following MyHttpModule assumption is reflected.
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 ("-- EndRequest is executed for the application's Event Response Method ");
}
Void HttpApplication_PostResolveRequestCache (object sender, EventArgs e)
{
Console. WriteLine ("application executes the -- PostResolveRequest" Event Response Method ");
}
Void HttpApplication_BeginRequest (object sender, EventArgs e)
{
Console. WriteLine ("application's event response method is executed -- BeginRequest ");
}

// Regard this method as an http request processing pipeline
Public void ProcessRequest (HttpContext context)
{
// 19 events, 23 steps
Console. WriteLine ("START request ");
// Trigger the first event
BeginRequest (this, null );
// Trigger 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 between 11 and 12 events
Console. WriteLine ("start execution page lifecycle ");
HttpHandler. ProcessRequest (context );
// 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
Copy codeThe Code is as follows:
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 is created based on the request
HttpContext context = new HttpContext ();
// Assume that it is created from HttpApplicationFactory
HttpApplication application = new HttpApplication ();
// Load all the HttpModule registered in the configuration file and execute its Init Method
Application. InitInternal ();
// Call the pr method to start executing the pr method on the page
Application. ProcessRequest (context );
Console. ReadKey ();
}
}
}

6. customize an HttpModule
Copy codeThe Code is as follows:
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 an 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 registers the EndRequest method ");
}
Void application_PostResolveRequestCache (object sender, EventArgs e)
{
Console. WriteLine ("HttpModule registers the PostResolveRequestCache method ");
}
Void application_BeginRequest (object sender, EventArgs e)
{
Console. WriteLine ("HttpModule registers the 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.