A friend wasMSNAsk me"ASP. NETFrom initial receiptHTTP RequestTo final generationResponseWhat is the overall process ?" I think this problem involvesIISAndASP. netasp. Net RuntimeThe model is not clearly explained in a few words, so I decided to write such an introduction.IISAndASP. NET runtime Process ModelOfArticleLet's talk about my rough understanding of this. If there is anything wrong with it, I hope you can correct it in time.
This article is divided into two parts. I will talk about the first part. IIS Two different versions- IIS 5.x And IIS 6(Although IIS 7 Already Release It has been a long time and has changed a lot since the previous two versions. IIS 7 For more information, see the following section: IIS How to monitor external sources HTTP Request , How ISAPI extension mapping For different Resource Requests are distributed to different ISAPI extension , Based on ASP. NET Resource Of ASP. NET ISAPI How to Set RequestPass ASP. NET Runtime Environment. The second part focuses on ASP. NET Runtime Environment HTTP Request . Let's take a look. IIS 5.x And IIS 6 .
1. I,IIS 5. x Based Process Model
IIS 5.x A notable feature is Web Server And real ASP. NET application. As Web Server Of IIS Run in Inetinfo.exe On the process, Inetinfo.exe Is Native Executive Is not a managedProgramAnd our real ASP. NET application Is run in Aspnet_wp Of Worker Process Above, the process will load CLR Therefore, this is a managed environment. Next we will talk about Aspnet_wp How to create, Aspnet_wpAnd Inetinfo.exe For more information, see Aspnet_wp , How Request Import ASP. NET rutime Pipeline .
We create Virtual directory Host To IIS In principle, we can use IIS Access all Resource , Which only contains some static resource files, such as slice and pure Html File, CSS , JSAnd so on. It also contains some files that require dynamic execution, such Aspx , Asmx Wait, we can also Remoting And WCF Service host To IIS . For these static files, IIS Extract the corresponding file and use it HTTP Response Return Client But for these dynamically executed files that need further processing, IIS You must set Request It is further passed to the corresponding processing program, and the final result is obtained after the processing program is executed. HTTP Response Pass IISReturn Client . For IIS For example, these handlers pass through ISAPI extension . For ASP. NET Of Resource , Corresponding ISAPI extension Is ASP. NET ISAPI , Through Aspnet_isapi.dll . IIS Of Metadata database Maintain ISAPI extension mapping Is responsible ResourceTo the corresponding ISAPI extension .
As we have shownIIS 5.x How to handle ASP. NET Resource ( Aspx For example) HTTP Request . First, the user passes Browser Request one ASPX page , Brower Forward Web Server That is, the target host's IIS. As we mentioned above, IIS Run in Inetinfo.exe In the process, Inetinfo.exe Is Native Executive Is not a managed program. IIS Analysis Request The extension of the target resource file (here is Aspx ), Through ISAPI extension mapping Obtain the corresponding ISPAI Is ASP. net isapi, Load Aspnet_isapi.dll . So far, RequestBy ASP. net isapi, Processing. ASP. NET ISAPI Creates Aspnet_wp.exe Of Worker Process (If the process does not exist ), Aspnet_wp.exe It is loaded during initialization. CLR To ASP. NET application Create a managed running environment CLR Initialization will load two important DLL : Appmanagerappdomainfactory And Isapiruntime . Pass Appmanagerappdomainfactory Of Create Method: Application Create Application domain ; Pass Isapiruntime Of Processrequest Processing Request And then drag the process ASP. net http runtime Pipeline Scope, ASP. net http runtime Pipeline Pair HTTP Request Processing is a relatively complex process, and relevant presentations are placed in the next part of this article. Here we can regard it as a black box, and it takes over Request , Finally generate Html.
This is basically the entire processing process, which is very simple. However, there are several points that need to be pointed out here.
1. First, only one host can run at a timeAspnet_wpProcess, each virtual directory-basedASP. NET applicationCorresponds toApplication domainIn other words, eachApplicationAll run in the sameWorker ProcessMedium,ApplicationThe isolation between them is based onApplication domainInstead of based onProcess.
2. Second,ASP. NET ISAPINot only createAspnet_wp Worker ProcessAnd monitors the process.Aspnet_wpOfPerformanceTo the lower limit of a specific setting,ASP. NET ISAPIStops the process. WhenAspnet_wpAfter the terminationRequestWill causeASP. NET ISAPICreate a newAspnet_wp Worker Process.
3. Finally IIS And Application When running in their respective processes, the communication between them must adopt a specific communication mechanism. Essentially IIS Location InetinfoProcess and Worker Process Communication between different processes of the same machine ( Local Interprocess Communications ), In Performance Between them, based on Named Pipe Communication mechanism. ASP. NET ISAPI And Worker Process Communication between them through a group Pipe . In Performance Reason, ASP. NET ISAPI Asynchronously Request Upload Worker ProcessAnd obtain Response , Worker Process It is synchronized ASP. NET ISAPI Obtain some Server .
2. II,IIS 6 Based Process Model
reliability and Performance never change the subject of software development. As host Based on HTTP application , these two aspects are particularly important. From IIS 5.x< span style = "font-family: "> to IIS 6 it is not difficult to see that IIS 6 the improvements made in the previous version are also based on these two aspects. Before introducing the processing model of IIS 6 , let's take a look at IIS 5.x< span style = "font-family: "> all kinds of defects:
1. FirstPerformanceSee,IISAndApplicationRunning in different processes, althoughNamed PipeBut the impact of inter-process-based communication on performance cannot be fundamentally solved.
2. SecondReliabilityYou can run only oneWorker Process, EachApplicationRunning in the same processApplication domainIsolation can provide a certain degreeReliabilityBut once a process crashes, allApplicationAre affected. Therefore, we sometimes need to provideProcessIsolation.
Based on Reliability , IIS 6 Introduced Application pool . As the name suggests, Application pool Is Application In IIS 6 , We can create several Application pool , In the create Web Application We specify an existing Application pool . When running Application Corresponds to Worker Process : W3wp.exe . That is to say IIS The difference is that IIS 6 You can run multiple Worker Process , Each Worker Process In Application domain Corresponds to Application . In this way, Application Can not only provide Application domain You can also separate Application Placed in different Application pool To ProcessIsolation. For Host Important Application This method can provide a good solution. Reliability .
in terms of Performance , IIS 5.x< span style =" font-family: "> inetinfo.exe listener request distribute request to Work Process . In other words, in IIS 5.x< span style = "font-family: ">, request is performed in User mode , in IIS 6 , this kind of work is transplanted to kernel mode . All of this is done through a 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 ( User mode ) and kernel mode ( kernel mode ). Generally, the user program runs under User mode , the Code of the operating system runs under kernel mode . kernel mode allows access to all system memory and all CPU commands. About User mode and kernel mode and some Windows underlying content, we recommend that you read Microsoft Windows internal four edition , authored by Mark E. russinovich & David. solomon .
It basically demonstratesIIS 6 The entire process. In User Mode , HTTP. sys Receives Aspx Of HTTP Request And then it will IIS In Metabase View Request Of Application Which Application pool , If Application poolIf it does not exist, it is created. Otherwise Request Send to corresponding Application pool Of Queue . As I mentioned above Application pool Corresponds to Worker Process : W3wp.exe Without a doubt, he is running in User Mode . In IIS metabase Maintenance Protection Application pool And Worker Process Of Mapping . Was( Web Administrative Service ) According Mapping , Will exist in Application pool queue Of Request Pass to the corresponding Worker Process ( If not, create such a process. ) . In Worker Process During initialization ASP. NET ISAPI , ASP. NET ISAPI Load CLR . The final process is like IIS 5.x Same: Pass Appmanagerappdomainfactory Of Create Method: Application Create Application domain ; Pass Isapiruntime Of Processrequest Processing Request And then enter the process ASP. net http runtime Pipeline .
PairIIS Process ModelThis section is introduced here. In the next section, I will introduceASP. net http runtime Pipeline.