Preface
Why do I have such a title? In fact, I only wanted to understand the asp.net pipeline model, but I checked other related materials horizontally when I checked the information, the gains are much larger than originally expected.
With the foundation of this article, we can better understand the following two articles:
Understand and customize HttpHandler
Understand and customize HttpModule
Directory
Generally, you do not need to write a directory. I feel that this time I want to write more clearly.
1.Asp.net pipeline model;
2. Sub-processes and threads of processes;
3. Application domain (AppDomain );
4. IIS5.x overall framework of the next HTTP Request/Response Process
5. Differences between IIS5.x, IIS6.x, and IIS7.x
Asp.net pipeline model
Reference: ASP. NET uses the pipeline model (PipleLines) to process HTTP requests
HttpRuntime understanding and deep understanding
Understanding of HttpModule (reproduced)
The MPs queue model contains the following objects:
Flowchart:
Http response), the Worker Process instance generates an HttpWorkerRequest object through ISAPIRuntime (the main function is to call some unmanaged code, the HttpWorkerRequest object contains all the information of the current request, and then passes it to HttpRuntime) the HttpWorkerRequest object is passed to HttpRuntime and the ProcessRequest method of HttpRuntime is called. The HttpRuntime is the entrance to the pipeline model and enters the pipeline model.
HttpRuntime generates HttpContext Based on the HttpWorkerRequest object. HttpContext contains attributes such as request and response, and then calls the GetApplicationInstance method of HttpApplicationFactory to generate HttpApplication. The HttpApplication object contains multiple HttpModule objects (when an HTTP request arrives at HttpModule, the entire ASP. the. NET Framework system has not processed this HTTP request. That is to say, HttpModule is the "Only Way" for an HTTP request ", therefore, some required information can be appended to the HTTP request information before the HTTP request is passed to the real request processing center (HttpHandler, you can also perform additional work on the intercepted HTTP request information, or simply terminate the HTT that meets certain conditions in some cases. P request, which can act as a Filter), and call the Init method of each HttpModule object to initialize HttpModule. In the Init method, you can subscribe to the event of HttpApplication for corresponding processing. When HttpApplication executes Application_ResolveRequestCache, it temporarily gives control to HttpHandler and determines whether to generate session tracking based on whether SessionState is enabled in HttpHandler (. set with enablesessionstate in aspx ,. in ashx, whether to inherit the IRequiresSessionState interface is used to set). Then, HttpApplication continues to execute its own events until the PreRequestHandlerExecute event is executed, and the HttpHandlerFactory object is obtained based on the suffix of the URL request. aspx calls System. web. UI. pageHandlerFactory ,. ashx calls System. web. UI. simpleHandlerFactory), call the GetHandler method of HttpHandlerFactory to generate a specific H The ttpHandler object or call the ReleaseHandler method allows the factory to reuse existing handler instances to process http requests and return http responses, after a series of events of the HttpApplication object (for specific events, refer to HttpModule (reproduced), they are finally returned to the client, of course, a series of http application events that pass through the http response can be subscribed to by the HttpModule object.
Sub-processes and threads of processes
Reference: Baidu Q &
Let me take Windows as an example, because the Linux kernel does not seem to have a thread concept. the difference between a process and a thread is that the granularity is different. variables (or memory) between processes cannot directly access each other. A thread can, and a thread must be attached to a process for execution. for example, if you open an IE browser in Windows, this IE browser is a process. when you open a pdf file in a browser, IE calls Acrobat to open the file. Acrobat is an independent process, that is, a sub-process of IE. IE itself uses the same process to open two web pages at the same time and runs the scripts on two web pages at the same time. The execution of these two web pages is implemented by IE itself through two threads. it is worth noting that the thread is still the content of IE, while the sub-process Acrobat strictly does not belong to IE and is another program. the reason is that the sub-process of IE is started only by calling IE.
Q: Can I understand it like this? The parent process creates a sub-process. As long as a task is assigned to the sub-process, it has nothing to do with it ....
A: You can't say that. It doesn't matter. The parent process can still get some information by communicating with the child process. Take the above example as an example,
IE can use some inter-process communication interfaces to check whether Acrobat has successfully opened the pdf file. However, I think your understanding is basically correct,
That is, the parent process and child process are independent. if IE runs a virus child process, the child process is not obedient, and the parent process has no special way, except to apply to the system to close it.
Partition Processes and threads are simple: the running of an independent program is called a process, and the different parts of concurrent execution in the process are called threads. another independent program running caused by this process is the sub-process of this process.
(Basically, we recommend that you refer to the operating system textbooks for stricter definitions)
Reference:. NET brief introduction to component programming (AppDomain application domain)
For more information, see http://blog.csdn.net/zhoufoxcn/article/details/2425420.
Process: A process occupies a memory address and is the boundary between an application and an application, processes cannot share code and data space (that is, they cannot directly interact with each other), but they can interact with each other through IPC (remoting, named pipe, webservice, and so on. An error or even a crash in a process does not affect the execution of other processes.
Sub-process: the sub-process is started by another process. The sub-process and the parent process are not affiliated. The two processes can interact with each other through IPC.
Thread: a concept in the operating system that defines the code execution stack and execution context. multiple threads in the same process share code and data space, but is only responsible for executing the code without carrying data. Independent threads or multiple threads are responsible for executing tasks in the process.
Question: there are still many aspects to explore the thread. For more information, see "Deep thread".
Application domain (AppDomain)
Reference: Understanding AppDomain
AppDomain is. the concept exclusive to the. net framework is a logical host. Its functions are as independent as processes (allocating independent memory space from processes, code and data space cannot be shared between AppDomains. When an AppDomain program encounters an exception or even crashes, it does not affect the programs running in other AppDomains. However, AppDomain is not a process. A process can have one or more AppDomains, and a default AppDomain must exist.
Maybe you have such a question: Is AppDomain a thread? If not, what is the relationship with the thread? In. net framework has three independent and related concepts: process, application domain (AppDomain), and thread. A process contains one or more AppDomains (a default AppDomain must exist ); A process contains one or more threads (usually a thread pool with multiple reusable threads); AppDomain has many-to-many relationships with threads, however, a thread can only process one AppDomain at a time, while an AppDomain can be simultaneously processed (concurrently) by multiple threads ).
The process from running the program is as follows: the system first allocates a memory address space, then gives control to CLR to generate the default AppDomain, and then loads the assembly to the default AppDomain, the program is officially run (the system does not have the AppDomain concept in the managed heap, And the AppDomain is not an operating system concept and is managed by CLR ). By default, AppDomain is generated with CLR and cannot be deleted or uninstalled by encoding.
The following figure describes the positional relationship between processes, threads, and AppDomain.
AppDomain cannot directly interact with each other. You can perform data interaction through proxy (if a process is used, IPC is used ). (The specific implementation will be discussed later !)
IIS5.x overall framework of the next HTTP Request/Response Process
IIS5.X web server on the left and Asp.net Application worker process on the right. Asp.net extends IIS as an IIS component.
Reference: Differences in ASP.net request processing in different versions of IIS
When an http request is sent to IIS5.X, IIS first converts the virtual directory to a physical directory, and then checks the metabase file in iis Based on the file suffix to check the file extension and executable code (Extension Program) ing records (such. aspx ,. if the metabase file does not exist, check whether it is a file that is not protected by the server (Files protected by the server: files in the App_Code folder; not protected by the server: the worker instance calls the corresponding executable code (aspnet_isapi.dll). aspnet_isapi.dll uses a named pipe (named pipe, a simple IPC-process communication mechanism. For details, see: named pipeline and extended process communication learning) from inetinfo. e The request obtained by xe is asynchronously forwarded to the Asp.net workflow instance: aspnet_wp.exe, and then enters the pipeline model. At the same time, aspnet_isapi.dll uses named pipeto monitor the job progress. If the job progress is less than a certain value, aspnet_isapi.exe will kill the job process. When the next request is passed, restart a job process to process the request. The working process synchronously requests web server information through named pipe (for example, the Server object is called to obtain server information ).
The figure still adheres to the ugly but useful principle !!
The working process of aspnet_wp.exe contains a thread pool and a default AppDomain. When a Request is sent to the working process, the working process will file the requested virtual directory (a virtual directory corresponds to an Application) the default AppDomain creates an AppDomain and loads the assembly of the virtual directory to the AppDomain. (more than one assembly may exist in the virtual directory, by default, the AppDomain will load all the assembly in the entire virtual directory to the AppDomain. If the AppDomain of the virtual directory already exists, the AppDomain will be used directly, if the assembly of the virtual directory changes (including web. config changes), a new AppDomain will be created, and then the changed assembly will be loaded into the new AppDomain; at this time, the idle thread execution assembly is obtained from the thread pool (write a website and publish it to two virtual directories for testing. We can see that the threads that execute http request processing are constantly changing, the two virtual directories will use the same thread ). The variables and statuses in the Assembly are stored in the memory of the AppDomain to which the Assembly belongs, such as HttpContext. current. items, Application, etc. (the Application object is actually a HashTable, which can be accessed by multiple threads (iis5.X) or multiple Application instances (iis6.x ), appDomain cannot directly access the other party's variables and status. The Session status variables include InProc, StateServer, and SQLServer. The default value is InProc, indicating that the Session status is saved in the Asp.net process, if the assembly of the virtual directory changes and the Session status variable previously set is called in the new AppDomain, the Session will be lost (the SessionID stored in the client's Cookie remains the same, if yes, the Session state variable is saved in the corresponding AppDomain when the Session mode is InProc.
Problem: if the session mode is set to StateServer, the Session status is saved by the Status server, that is, the Session status is saved by another local or remote process, steps for enabling a local server (Windows server type): 1. start-> All Programs-> Administrative Tools-> services-> enable the Asp.net status service, and then configure the website's web. config is <sessionState mode = "StateServer" stateConnectionString = "tcpip = localhost: 4242"/>
Differences between IIS5.x, IIS6.x, and IIS7.x
Reference: Differences in ASP.net request processing in different versions of IIS
IIS5.x is designed as a server that enables only one worker process to process all requests/responses. To ensure that each Application is in a virtual directory) independent operation without interfering with other applications (an Application crash does not cause the entire process to crash), The AppDomain is introduced. However, the AppDomain effect was unsatisfactory, so IIS6.x began to use the Application Pool ). In non-Web Garden mode, the next Application corresponds to an Application pool and a worker process. x starts to change the working process from Aspnet_wp to w3wp. In Web Garden mode, an Application corresponds to an Application pool and multiple working processes. The Application can be executed on any working process, once one of the worker processes crashes, the Application requests can be processed in a timely manner. However, SessionState in Web Garden mode cannot use InProc mode, because SessionState is saved in the AppDomain of the worker process.
IIS5.x identifies the Application that the request belongs to in the working process in the user mode, while IIS6.x is implemented by the http of the Web Server. sys is implemented in the core mode (IIS5.x is Aspnet_isapi.dll, and IIS6.x starts to use the new component http. sys ). Note: To prevent users' applications from accessing or modifying critical operating system data, windows provides two processor access modes: User Mode and Kernel Mode ). Generally, the User program runs in User mode, while the operating system code runs in Kernel Mode. The Kernel Mode code allows access to all system memory and all CPU commands.
IIS5.x and IIS6.x ASP. NET is added to IIS in the form of iis isapi extension, while IIS7.x inherits Asp.net to IIS, and IIS7.x has two working modes: Classic mode and integration mode. For details, refer.
Summary
This article is based on the summary of my big brother's organization and summary, and will be further improved in the future !!
Mark the source for reprinting! Http://www.cnblogs.com/fsjohnhuang/archive/2012/07/12/2587658.html