ASP.NET 學習4_IIS pipeline_PrePage執行架構_Webform生命週期



IIS pipeline

在版號7以上的IIS有提供兩種處理ASP.NET Request的模式(不影響webform的Page Life Cycle)
(1)Integrated mode(default):
全新提高速度和穩定性的模式,可允許.NET開發者
ASP.NET visual studio中編寫一些功能(ASP.NET Module)
改變IIS的行為,不再通過ISAPI的方式。至於擴充套件,則可以使得我們對於IIS,以及其他型別的請求有更多的控制。
-->Visual Studio中專案種類:ASP.NET Module ,
或是選擇Class Library實作IHttpModule interface也可以


(2)Classic:
指的是相容IIS 6或之前版本的一種模式,一個典型問題就是,在處理
ASP.NET這種動態網站的時候,它是通過一個所謂的ISAPI程式,作為外掛的方式來工作的。針對不同的動態應用程式(例如ASP,PHP等),會需要
不同的ISAPI(Internet Server Application Programming Interface)。

(備註:ISAPI是用來擴充IIS伺服器功能所用的技術,是用
C/C++來編輯的,產生的檔案為dll檔,可供IIS伺服器調用。)




Pre-Page Execution Architecture
(Integrated Mode)


這一段過程,講白話就是告訴你我在IIS轉換成應用程式背後的運作原理

首先http.sys是一個protocol listener
負責監聽HTTP請求,也負責response給Client端的browser。
而當接收完請求會再傳給IIS進行下一步處理(比方要分配給哪個application、process)。
此時下一步該怎麼做就會有一些相關組態設定透過

(1)WAS(Windows Process Activation Service):Windows 處理程序啟用服務 (.exe檔案)
WAS會先去讀applicationHost.config去判斷對應的app pool是否有開啟。
若沒有會自行產生新的實體w3wp worker process。
再將組態資訊傳給W3SVC處理。
(2)applicationHost.config : 給WAS讀取,會記錄
對應Request的application pool及目標應用程式進來處理。
(3)W3SVC(World Wide Web publishing service)  (.exe檔案)
是http.sys介面的listener,用來確保http.sys獲取的是最新的組態設定,能將HTTP要求轉送到適當的應用程式集區或作業系統的行程。如果停止這項系統服務,Windows將無法為任何形式的Web要求提供服務。

三個元素(缺一不可)。

接著Http.sys會準備一個Queue 用來裝每一個Request,等其準備好就會
依序傳遞給特定某一個Worker Process執行後續處理。

於WorkerProcess當中一開始就會傳給Integrated Pipeline執行一系列的Event階段處理
各階段會自行註冊對應的原生IIS Managed .NET HTTP Modules(一些要調整、修改IIS的事情)
而每一個註冊的Module要執行時未透過ManageEngine代理(Event Proxy)
來進行運作,產生相應的Application Domain下的ApplicationManager執行實體
確保任務之間在同一個Worker Process之中互不影響,獨立開來。

ApplicationManager中會產生一系列HTTPRuntime,HTTPContext,HTTPApplication
最終匯集成一個Application集合。


接著當我們發送請求給IIS要求呈現我們的網頁時候
就會是如下運作(兩張圖皆一樣意思)



 ASP.NET 請求處理過程是基於管道模型,這個管道模型是由
多個HttpModule和HttpHandler組成。

ASP.NET 把HTTP Request依次傳遞給管道中各個HttpModule,最終被HttpHandler處理。
處理完成後,再次經過管道中的HttpModule,把結果返回給客戶端。

Client 端發送request到IIS server時,.NET的處理大致上可分兩步驟:
Step1.創建一個能夠處理request以及HTTP相關的一個process。
會產生一個可處理request、response以及context對象的應用進程。

IIS會檢查哪一個ISAPI Extension能夠處理這個request,這取決於文檔的後綴名。
比如 .aspx就會被傳遞到 aspnet_isapi.dll來進行處理。

如果這是針對該網站的首次request,會由’ApplicationManager’類別 創建一個該網站可以運行的應用程式定義域(App Domain)。該定義域隔離部署在同一台IIS server上的兩個不同的Web Application,所以即使其中一個定義域中的程式出現了錯誤,也不會影響其他定義域的正常運作。

Step2.一旦Process被創建,request就會通過由HttpModule、Http Handler 
和page objects 所觸發的一系列事件。
為方便記憶我們可將這些流程稱為: MHPM(Module、Handler、Page and Module Event)。




一樣背景執行服務程序會有W3SVC跟WAS,透過http.sys一接一回。
當進入到Integrated pipeline時候就會一直執行上述的動作
每一個Request都會於Application Domain產生一個Thread


在IIS中若想看Managed Modules則在此(dll類型)

有些Module Type為Native 的就是來自於applicationHost.config
的Global設定


這些設定預設都不會出現在web.conig中,因為Entry Type為Inherit

若是改為Local則會呈現在專案的web.config中


當全部執行完才會正式接續去跑一個Page Life Cycle




(Classic Mode)

在古早的Classic模式中架構則是要Load進ISAPI extension來處理asp.net的code,
而處理modelus方式都是用.net files方式,因此耗費相對多的時間。



Webform生命週期

Webform Life Cycle
https://docs.microsoft.com/zh-tw/previous-versions/visualstudio/visual-studio-2008/ms178472(v=vs.90)?redirectedfrom=MSDN



Page Life Cycle(一般網頁生命週期八階段)

Step1.網頁要求(Page Request)
Step2.啟動(Start)
Step3.初始化(Page Initialization)
Step4.載入(Load)
Step5.驗證(Validation)
Step6.事件處理(PostBack Event Handling)
Step7.顯示(Render)
Step8.卸載(Unload)

記憶訣竅:英文單字(silver)
Request -> SILVER -> Unload



Following are the different stages of an ASP.Net page:
  • Page request . when ASP.Net gets a page request, it decides whether to parse and compile the page or there would be a cached version of the page; accordingly the response is sent
  • Starting of page life cycle . at this stage, the Request and Response objects are set. If the request is an old request or post back, the IsPostBack property of the page is set to true. The UICulture property of the page is also set.
  • Page initialization . at this stage, the controls on the page are assigned unique ID by setting the UniqueID property and themes are applied. For a new request postback data is loaded and the control properties are restored to the view-state values.
  • Page load . at this stage, control properties are set using the view state and control state values.
  • Validation . Validate method of the validation control is called and if it runs successfully, the IsValid property of the page is set to true.
  • Postback event handling . if the request is a postback (old request), the related event handler is called.
  • Page rendering . at this stage, view state for the page and all controls are saved. The page calls the Render method for each control and the output of rendering is written to the OutputStream class of the Page’s Response property.
  • Unload . the rendered page is sent to the client and page properties, such as Response and Request are unloaded and all cleanup done.



Page Life Cycle Event (頁面生命週期事件種類)






  • PreInit . PreInit is the first event in page life cycle. It checks the IsPostBack property and determines whether the page is a postback. It sets the themes and master pages, creates dynamic controls and gets and sets profile property values. This event can be handled by overloading the OnPreInit method or creating a Page_PreInit handler.
  • Init . Init event initializes the control property and the control tree is built. This event can be handled by overloading the OnInit method or creating a Page_Init handler.
  • InitComplete . InitComplete event allows tracking of view state. All the controls turn on view-state tracking.
  • LoadViewState . LoadViewState event allows loading view state information into the controls.
  • LoadPostData . during this phase, the contents of all the input fields defined with the <form> tag are processed.
  • PreLoad . PreLoad occurs before the post back data is loaded in the controls. This event can be handled by overloading the OnPreLoad method or creating a Page_PreLoad handler.
  • Load . the Load event is raised for the page first and then recursively for all child controls. The controls in the control tree are created. This event can be handled by overloading the OnLoad method or creating a Page_Load handler.
  • LoadComplete . the loading process is completed, control event handlers are run and page validation takes place. This event can be handled by overloading the OnLoadComplete method or creating a Page_LoadComplete handler.
  • PreRender . the PreRender event occurs just before the output is rendered. By handling this event, pages and controls can perform any updates before the output is rendered.
  • PreRenderComplete . as the PreRender event is recursively fired for all child controls, this event ensures the completion of the pre-rendering phase.
  • SaveStateComplete . state of control on the page is saved. Personalization, control state and view state information is saved. The HTML markup is generated. This stage can be handled by overriding the Render method or creating a Page_Render handler.
  • UnLoad . the UnLoad phase is the last phase of the page life cycle. It raises the UnLoad event for all controls recursively and lastly for the page itself. Final cleanup is done and all resources and references, such as database connections, are freed. This event can be handled by modifying the OnUnLoad method or creating a Page_UnLoad handler.


當Page為第一次load時候的

當發生PostBack時候(非第一次載入)的Life Cycle















Ref Link:
ISAPI簡介
https://addfdsf.pixnet.net/blog/post/58023164-isapi%E7%B0%A1%E4%BB%8B

如何理解IIS 7的兩種應用程式池的管道模式(Managed Pipeline Mode)
https://www.itread01.com/content/1545745717.html

啟動或停止網頁伺服器 (Windows Server 2012 R2)
http://shaurong.blogspot.com/2016/02/windows-server-2012-r2.html
https://ithelp.ithome.com.tw/articles/10078673
https://blog.csdn.net/xw505501936/article/details/48339243

What is w3wp.exe? Learn the Basics About IIS Worker Processes
https://stackify.com/w3wp-exe-iis-worker-process/


How to work with HttpModules in ASP.Net
https://www.infoworld.com/article/3090997/how-to-work-with-httpmodules-in-aspnet.html

ASP.NET Application and Page Life Cycle
https://www.codeproject.com/Articles/73728/ASP-NET-Application-and-Page-Life-Cycle-2

ASP.NET 網頁存留週期概觀
https://docs.microsoft.com/zh-tw/previous-versions/visualstudio/visual-studio-2008/ms178472(v=vs.90)?redirectedfrom=MSDN








留言

這個網誌中的熱門文章

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header

經得起原始碼資安弱點掃描的程式設計習慣培養(三)_7.Cross Site Scripting(XSS)_Stored XSS_Reflected XSS All Clients

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題