. NET (4) Application Object HttpApplication, office object application library
After an HttpContext object is created, HttpRuntime creates an object for processing requests. The object type is HttpApplication.
In ASP. NET, HttpRuntime manages a definition in System. for an instance of the HttpApplicationFactory class in the Web namespace, HttpApplicationFactory manages the HttpApplication object in factory mode and maintains an HttpApplication Object pool in HttpApplicationFactory so that the created HttpApplication object can be reused, however, each HttpApplication object is only used to process one request at a time. In this way, for ASP. NET programmers do not need to consider the concurrent processing of multiple requests in HttpApplication.
In actual request processing, we still need to do a lot of work, for example, checking which user initiated the current request, in this way, we can perform different processing for different users, or decide whether to process user requests based on the user, and return a response that lacks the corresponding permissions for users who do not have permissions. If we complete these tasks in one method, it will obviously lead to over-bloated methods. In HttpApplication. the event mechanism in. NET breaks down the processing process into multiple steps by sending multiple events in sequence. This processing mechanism is usually called a processing pipeline, next we will discuss in detail the internal mechanism of processing pipelines and multiple events of HttpApplication.
1. MPS queue
The so-called pipeline Processing refers to decomposing the processing process into multiple processing steps when dealing with complicated problems. We refer to this processing method after multiple steps as processing pipelines. In. with the powerful power of events, we can encapsulate complex processing steps through processing pipelines, multiple steps of the processing process are provided to the programmer through the event so that the programmer can expand the management pipeline. For example, an overview of how HttpApplication processes pipelines.
For an MPS queue, a large number of events are often exposed. These events provide a programmer's extension mechanism. However, for a class with a large number of events, defining a large number of events means that the cost of creating an event is required when you create an object. in. NET, the so-called event is a restricted delegate member. Defining multiple events means that more storage space is required for the created object. To solve this problem. componentModel. the Component class provides the basis for processing multiple Events: the Events attribute. Its type is System. componentModel. eventHandlerList is a linear dictionary. When an event is required, the event is saved to the collection using the key. If no corresponding event exists, the event creation cost will not be paid, eventHandlerList allows you to manage multiple event objects in a collection to save space occupied by objects. Its main members are as follows:
In use, a class is first derived from Component, which inherits the Events set attribute of Component. For each class to be defined in this derived class, this class inherits the Event set attribute of Component. For each event that needs to be defined in this derived class, a corresponding Key object is defined in the class. Later, the Key object is used to access the Events managed by the Events set.
Programmers can define a similar dictionary in the class to complete this task. It is not necessary to derive from the Component class. As follows:
Public class ProcessPipeline: System. componentModel. component {# region private static readonly object startEvent = new object (); private static readonly object preProcessEvent = new object (); private static readonly object postProcessEvent = new object (); private static readonly object endEvent = new object (); # endregion # region public event EventHandler StartProcess {add {this. events. addHandle R (startEvent, value);} remove {this. events. removeHandler (startEvent, value) ;}} public event EventHandler PreProcess {add {this. events. addHandler (preProcessEvent, value);} remove {this. events. removeHandler (preProcessEvent, value) ;}} public event EventHandler PostProcess {add {this. events. addHandler (postProcessEvent, value);} remove {this. events. removeHandler (postProcessEvent, value );} Public event EventHandler EndProcess {add {this. events. addHandler (endEvent, value);} remove {this. events. removeHandler (endEvent, value) ;}# endregion # region protected void OnStartProcess (EventArgs e) {if (this. events [startEvent]! = Null) {(this. Events [startEvent] as EventHandler) (this, e) ;}} protected void OnPreProcess (EventArgs e) {if (this. Events [preProcessEvent]! = Null) {(this. Events [preProcessEvent] as EventHandler) (this, e) ;}} protected void OnPostProcess (EventArgs e) {if (this. Events [postProcessEvent]! = Null) {(this. Events [postProcessEvent] as EventHandler) (this, e) ;}} protected void OnEndProcess (EventArgs e) {if (this. Events [endEvent]! = Null) {(this. events [endEvent] as EventHandler) (this, e) ;}# endregion public void Process () {Console. writeLine ("Start processing"); this. onStartProcess (EventArgs. empty); Console. writeLine ("prepare for processing"); this. onPreProcess (EventArgs. empty); Console. writeLine ("is being processed... "); This. OnPostProcess (EventArgs. Empty); Console. WriteLine (" processing ended "); this. OnEndProcess (EventArgs. Empty );}}
Namespace Custom Event pipeline {class Program {static void Main (string [] args) {ProcessPipeline process = new ProcessPipeline (); process. startProcess + = new EventHandler (process_StartProcess); process. startProcess + = new EventHandler (process_StartProcess1); process. startProcess-= new EventHandler (process_StartProcess); process. preProcess + = new EventHandler (process_PreProcess); process. postProcess + = new Eve NtHandler (process_PostProcess); process. endProcess + = new EventHandler (process_EndProcess); process. process (); Console. readLine ();} static void process_StartProcess (object sender, EventArgs e) {Console. writeLine ("processing in the event to start processing... ");} Static void process_StartProcess1 (object sender, EventArgs e) {Console. writeLine ("processing 1 In the event before processing");} static void process_PreProcess (object sender, EventArgs e) {Console. writeLine ("processing in the previous event... ");} Static void process_PostProcess (object sender, EventArgs e) {Console. WriteLine (" Processing Event .... ");} Static void process_EndProcess (object sender, EventArgs e) {Console. WriteLine (" processed event processing... ");}}}
In the ProcessPipeline class, four events are defined. First, you must note that four actual delegate members are not defined in the class to correspond to events. By deriving from Component, in fact, the base class inherits an EventHandlerList Member, defines the event as an attribute, and maps the event to the EventHandlerList regardless of how many events are defined in the class, in the created object instance, you only need such an EventHandlerList object to save the delegate object of the Event Response.
Secondly, the Process is simulated in the Process method. During the Process, the method starting with On that triggers the event is called, expose the processing steps to programmers in the form of events.
When using this class, you can register the event processing method to handle the event that programmers are concerned about. In the process of ProcessPipeline processing, the corresponding event processing method is called.