1 Questions:
If a httpmodule is registered in the system and the code is written in BeginRequest, does it affect the execution of other requests if it is stuck?
Check out the MSDN explanations:
Http://msdn.microsoft.com/zh-cn/library/system.web.httpapplication (vs.80). aspx
Instances of the HttpApplication class are created in the ASP. NET infrastructure, not directly by the user. An instance of the HttpApplication class is used to process multiple requests during its lifetime, but it can only process one request at a time. This allows the member variable to be used to store data for each request.
According to the description here can not answer if there are 2 request at the same time what to do? First event beginrequest of Request1 is executed first, and the first event beginrequest of Request2 is completed after processing? If the BeginRequest event that handles Request2 is stuck, can the other request continue to execute?
21 General process of request execution
The first step of the ASP. NET execution request is the Httprumtime.processrequest method, which then internally httpapplication the instance and executes the HttpApplication. ProcessRequest method, and finally calls the HttpHandler ProcessRequest method.
Reflection looks at the source code, the core function of httpruntime:
The BeginProcessRequest function of HttpApplication
Resumesteps function:
When is the data for the _execsteps array added? is when the HttpApplication is initialized:
First look at where to put the current HttpApplication into the freelist, positioning to the end of the Resumesteps function:
The interior also writes a small logic that does not retain more than 100 instances of HttpApplication.
Then we look at the function initinternal and see how our _execstep array is initialized:
In each module, the incoming instance is registered for events. Like what:
Next, the delegates for each event are programmed into steps:
To view the Createeventexecutionsteps function:
When debugging, you can see what's in the steps:
3 Summary: The overall flowchart is as follows:
- Httprumtime.processrequest is called
- Get an example of HttpApplication
- Take an instance from an idle list or create an instance of a new HttpApplication
- Call the Initinternal method to initialize the
- Call Initmodule to initialize the
- Get each HttpModule
- Calling the Init method of HttpModule
- Each httpmodule defines the delegate that corresponds to the event in the Init method
- Incorporate system built-in processing steps into _execsteps
- The corresponding delegate of each event is programmed into the _execsteps
- Call Httpapplication.beginprocessrequest
- Calling the Resumestep method
- Perform the steps in the _exesteps array one by one
- When you're done, recycle this instance
4 Back to my question:
At the same time there are 2 request in, the system will produce 2 HttpApplication instances, each instance processing one, 2 will not affect each other.
And look at MSDN's exact words:
Instances of the HttpApplication class are created in the ASP. NET infrastructure, not directly by the user. An instance of the HttpApplication class is used to process multiple requests during its lifetime, but it can only process one request at a time. This allows the member variable to be used to store data for each request.
The application executes the events handled by the module or user code defined in the Global.asax file in the following order:
What is called being used to process multiple requests?
That is, the instance is recycled, and after the request is processed, other requests can be processed.
What do you mean only one request can be processed at a time? What if there are multiple requests?
Multiple requests generate multiple instances!
How do I control the order in which events are executed?
Make a list of the step inside, add it in a fixed order, and then execute it again in a sequential order.
It seems that I have this question is the key to be affected by the application instance only 1, so look at the MSDN this sentence, half a day did not understand suddenly said to deal with a number of requests, said to deal with a request, read the source code, finally understand why he suddenly write can handle multiple requests, You can only process 1 requests at a later write. Yes, a Web application has only 1 application instances, but that refers to the application attribute on HttpApplication, not the 1 instance of HttpApplication, and the number of concurrent requests. There are a number of instances of HttpApplication.
5 debugging proves that HttpApplication instances are produced in multiple, and can be reused:
Write a httpmodule with the following code:
public class httpmodule1:ihttpmodule{public void Dispose () { } public void Init (HttpApplication Context) { context. BeginRequest + = new EventHandler (context_beginrequest); } void Context_beginrequest (object sender, EventArgs e) { if (((HttpApplication) sender). Request.RawUrl.EndsWith ("default.aspx")) { thread.sleep (20000);}} }
Configure it to the Web. config:
< httpmodules > < name= "HttpModule1" type= "HttpModule1"/> </httpmodules>
Simple addition of 2 aspx files: Default.aspx and Test.aspx. Then start debugging:
1) Request Default.aspx
The first default.aspx request arrives, the instance is 971734, let it continue execution, stuck.
2) Open a second browser, request test.aspx
The second Test.aspx request arrives with an instance of 45150396, inconsistent with the previous one, stating that there are multiple instances of HttpApplication when a concurrent request arrives.
3) After 20 seconds, request test.aspx:
The instance changes back to 971734, stating that the previous instance of executing default.aspx was not destroyed and reused here.
How does the HttpApplication event execute when a concurrent request is performed?