Previous Article <in-depth analysis of ASP. NET Component Design> Chapter 3 about ASP. the NET operating principle is supplemented with a general overview of the structure and process. After reading this, I believe that I can understand the overall process. however, many details, such as how to create an HttpApplication object in HttpRuntime, are still unclear.
Of course, understanding these details is not necessary. Even if you do not know how the HttpApplication object is created, you can still create components. however, if you study these details, I believe you will absorb a lot of technical nutrients. for example, through the HttpApplication object creation process described in this article, you can deeply understand ASP. NET Design, and you can learn how to use the Factory design pattern. This is another feature of Mr. Huang's book, that is, he not only talked about ASP. NET and some programming methods and skills. For beginners who learn the design mode and find it hard to understand the design mode, by reading Mr. Huang's book, you can see a lot of practical explanations on the design model.
In fact, HttpApplication is not created by HttpRuntime. httpRuntime only requests HttpApplicationFactory to return an HttpApplication object. after receiving the request, HttpApplicationFactory first checks whether the request exists and is idle (can this be said? HttpApplication object. If yes, an HttpApplication object is retrieved from the pool and returned to HttpRuntime. If no, an HttpApplication object is created to HttpRunTime, like fish farming, every fish is an HttpApplication, And You Are HttpApplicationFactory. If someone asks you to fish, you will check whether there is a ready-made and suitable fish in the pool, create a fish, put it in the pool, and give it to others if you don't have one. It sounds good. You seem to be very powerful)
As a matter of fact, we can zoom in the creation process of HttpApplication to see more details. the above describes how HttpApplication is requested and how it is returned to HttpRunTime. In summary, HttpRunTime does not directly create an HttpApplication, but gives the created right to HttpApplicationFactory, the Factory mode is actually used here, and it is not the general Factory mode, but the Factory mode with the Pool capability. the HttpFactory object is responsible for creating and caching the HttpApplication object and returning the appropriate object to HttpRuntime. there is a problem here, that is to say, at the same time, there may be many HttpApplication objects in the "fish pond" of HttpApplicationFactory. how many HttpApplication objects can he cache at most? By default, the maximum number of HttpApplication objects to be cached is 100, and HttpApplicationFactory cyclically releases the HttpApplication objects that exceed this number (does that mean that more than 100 people access the system at the same time, no matter how good the design is, will it also encounter performance bottlenecks ?)
The process of creating HttpRuntime in HttpApplicationFactory is a process of Parse and Compile. in the original book, Mr. Huang said that HttpApplicationFactory will use Parser objects to parse Global. asax, at the same time, load Global. dll file. At the same time, create a source code that inherits from this class, use the Compiler object to compile the source code, and then create an HttpApplication object. this can also be seen from the diagram in Mr. Huang's book. however, it is estimated that many people are dizzy here. you can understand this process as follows:
First, the Parser and Compiler objects mentioned by Mr. Huang are actually two instances of the Codeparser and CodeCompiler classes. They are.. net class library, Codeparser object is used to convert a piece of text into a section of C # Or VB source code
(
Can't you understand it? Have you ever wondered what the ASP. NET control defined in <ASP: Label runat = server> </ASP: Label> On the ASPX page will become during runtime? The Parser object can be used to parse the code in <asp: Label> and similar controls and <script runat = "server"> into the corresponding C # source code snippet, that is, in the form of Label label2 = new Label ();, it returns a CodeCompileUnit object.
If you still do not understand ASP. NET, you do not understand ASP. NET controls. you must remember that, processing the corresponding ASP. NET page request is not ASP. NET is not IIS, not ASP. NET page itself. but ASP. NET, which uses the PARSER object to convert the control tag to the C # source code and is derived from the PAGE class and compiled and implemented. this is very important. Unlike ASP, we know that ASP code is loaded into the memory and parsed by ASP runtime and then returns HTML. However, ASP. NET is not, ASP. NET is an instance of a class that can output HTML. similarly, Parser is also used during the creation of the HttpApplication object, because HttpApplication actually depends on Global. ASAX file, and this file, we know, if you don't use CodeBehind to write it, it is a <Script Runat = "Server">. In other words, it is also a mark, this mark must be converted to C # source code, compiled into a class, and then an instance of this class is generated. This class is HttpApplication. in fact, CodeDom technologies such as CodeParser, CodeCompiler, and Reflection are. one of the core parts of. NET ,. NET is very dependent on these parts)
Why does parser parse Global. ASAX has to load Assembly again in the same place, which many may not be able to figure out. because if CODEBehind technology is used, all the code is included in Assembly. Why do we need to parse Global. what about ASAX? If you think so, you will be wrong. First, it may also be Global. ASAX defines some functions, while Assembly defines another part. Secondly, no one calls Gloabal. ASAX only allows code, and some people like to use the OBJECT tag to define objects in the APPLICATION range or Session range. Therefore, parser must parse Global. the ASAX file converts it to a C # code snippet, loads the Assembly, and uses the Reflection technology to create source code for a new class inherited from class Global (default class name, then, combine the Source code of the two parts (Ghost Application Class Source), use Compiler to create a new Class (Ghost Application Class Assembly), and generate an instance of the new Class to return it to HttpRuntime.
In the original book, Mr. Huang said Parser would load Global. dll file, I think, this may be a mistake. Many people do not understand Global. what is dll. I have written ASP. NET users know that there is no such file at all. I think, Global. dll contains global. the Assembly generated by the project compilation of the asax file, because this Assembly contains information about the Global class in the CodeBehind mode. therefore, Parser needs to load it.
At this point, the HttpApplication object has been created. In addition, the following describes the number of objects such as AppDomain, HttpRuntime, HttpContext, HttpSession, HttpApplication, and HttpModule, and their correspondence with the number of users.
Each ASP. NET has only one AppDomain, and each AppDomain corresponds to one httpRunTime, which is irrelevant to the number of users.
Each user corresponds to an HttpApplication, an HttpSession, an HttpContext, and a group of httpmodules.
If the Parser and Compiler in the process of generating the HttpApplication object are unclear (this part is very important, including the AspX page processing method), the following figure may help you understand: