[Reading Notes] component design-httpapplication details

Source: Internet
Author: User
This chapter is the foundation and spirit of the book. The subsequent example chapter aims to verify the content described in this chapter and practices.

The first section describes ASP. net running mode, this section focuses on the entire ASP. NET application operating mode, in fact, is not talking about components, but is very important, because the person who writes components must know ASP clearly. NET application startup. how to handle requests, how to deal with session and other details, but this section may be obscure to general readers. the following explanation may help you understand everything. an ASP. net Applications start with IIS. when you request a request containing ASP. when IIS receives the request (IIS is a Web service waiting process), after IIS receives the request, the site is located based on the host header, IP address, or port number requested by the requester. after the site is found, if the requested resource is a webform ending with aspx, IIS will give control to an ISAPI extension ., the name is aspnet_isaip.dll. at this time, the control is transferred from IIS to the ISAPI extension of ASPnet ., it must be noted that the ISAPI extension level is lower than IIS, but higher than the user site Point, it is independent from the site after the ISAPI receives the Processing request, it will start an ASP. net workflow. then, the requester's request information is forwarded to ASP. net workflow (named aspnet_wp.exe ). next, the control is controlled by aspnet_wp. aspnet_wp first solves the information of the requester, if the requester requests ASP. NET application (site or virtual directory, popular) does not have appdomain, aspnet_wp will create an appdomain, and will be requested ASP.. NET application assembly (that is, those DLL, such as system. web. DLL and so on) load to appdomain the above steps can see a conclusion and law: the control is in the form of a flow between each request handler, and, the previous requestor must be responsible for transmitting the information required by the next requestor. it is also responsible for loading or initializing the last handler, which is similar to the relay race in our life. the problem is that many people may ask: why is it so complicated? How can I transfer a request directly to aspnet_wp through IIS? It is not impossible, but because of this, the scalability of this processing process is reduced. ASPnet ISAPI is a bridge between IIS and aspnet_wp. although it seems that it is only responsible for forwarding requests and other work. in this way, the scalability is greatly extended. another question is about appdomain, including me. I had a misunderstanding of appdomain at the beginning, and Microsoft talked about appdomain in a vague manner. Some people say it is the same as the process, however, I first understood it as an application pool in IIS, so I took a lot of detours. In fact, appdomain is neither a process nor an application pool in IIS .. all applications under. net run in the appdomain (I understand it myself). Each appdomain is Run Every time an application or ASP.. NET application ,. net execution environment will create an appdomain, and then load some DLL required by the application. appdomain functions are similar to processes, but they are never processes. you can understand that appdomain is ASP.. NET application execution environment. aspnet_wp is not only responsible for creating appdomain (if it already exists, use this domain directly), but also, after the appdomain is created, the request is also forwarded to the isapiruntime object in the corresponding appdomain. (The isapiruntime object is part of the appdomain ). Isapiruntime is responsible for solving the necessary information of the request. It transfers information and requests to httpruntime. Here, it should be noted that isapiruntime is a class, its full name is system. Web. Hosting. isapiruntime, and httpruntime is also a class, its full name is system. Web. httpruntime. Therefore, we can say that these two objects are part of the appdomain runtime environment. When aspnet_wp is used to establish appdomain, it will also be used as the runtime environment to create these two objects. since several objects are described one after another, when I read this book for the first time, especially when I saw it, I felt very dizzy because of the first pair. net Framework class library is not familiar with, second, Asp. net operating principle first contact. I can't figure it out. I always want to match these object names with a DLL or a real file. in fact, whether isapiruntime or httpruntime, they are instantiated as part of the appdomain when the appdomain is created. so they represent an instance of a class in the memory, that is, an object. in addition, some of the above operating principles seem to follow ASP. NET application is not directly linked. it seems that the question is not correct, and it is easy for beginners to refer to the cloud. in fact, from IIS to ISAPI is the first request, that is, accepting the customer request. from ISAPI to Ppdomain, which is the second part, that is, the initialization part, aims to establish a large environment for processing requests, for the following processing requests and running ASP. NET application. next, after the appdomain Initialization is complete, you need to establish a session. Therefore, the request is accepted by httpruntime. The main task of httpruntime is to create an httpcontext object for each customer who initiates the request. this stuff manages the httpsession object. each visitor has their own httpcontext object and httpsession object. net Framework library, such as system. web. httpcontext, system. web. httpsessionstate. it can be seen that the request processing process is very similar. process of event model processing in. net. several processing modules are concatenated to an event. in ASP. in the net operating principle, also, several modules process one request in turn, such as pipeline operations.. As a component developer, you must specify the hierarchy and call creation relationships of httpruntime, httpcontext, and httpsession objects. you do not need to know the details. As long as you know who created and who was called, httpruntime is responsible for creating httpcontext and httpsession, and httpcontext is responsible for managing httpsession until httpruntime has created httpcontext. In fact, your application is still not running, or the requester's request is not actually processed. The previous work is both preparation or auxiliary work. in addition to the above object, httpruntime also creates httpapplication. the process of creating an application object is complicated. next, httpapplication calls the processrequest method to process user requests. This method calls the corresponding HT Tphandler is used to process user requests. httphandler processes requests based on the file extension of the user request, and sends the request result, that is, HTML, to the client browser, the complexity of the process is far beyond the above description. Basically, Mr. Huang described ASP in Chapter 3, Section 1, using a dozen pages of text. net running process and principle, as well as some methods used to process requests, but the general process is as described above,, I did not strip the details of object creation to show to you. the content in Mr. Huang's original book is actually very detailed. but why do everyone seem to be struggling? On the one hand, this is because the principle has always been quite troublesome. On the other hand, it is because Mr. Huang did not describe the process and program to everyone before describing the details, then, the details and program are directly integrated. in this way, if the subtitles and content in this section are not linked together and the program is first summarized. after reading it, you will feel dizzy. in fact, the whole story is about ASP.. Net process the request. if you hide all technical details and only talk about the process, you may soon understand it. then I will show the technical details of each part of the process. I think it is much easier to understand. this is like telling a story. It is better to outline the story first. of course, I am not saying that Mr. Huang is not writing well. In fact, this section is very well written and can be easily understood. the process is very important. It is important that you know when something happens, and you can do some processing at the specified time point. in this regard, I will introduce ASP in later chapters of Mr. Huang. NET page object execution process is more important. the following figure shows the entire ASP. the functions and processes of each object during the net application running process are illustrated. of course, the illustration discards technical details, such as how to create an httpapplication.

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:

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.