http 協議之所以能夠獲得如此大的成功,其設計實現的簡潔性和無狀態連接的高效率是很重要的原因。而為了在無狀態的 http 請求和有狀態的客戶端操作之間達到平衡,產生了服務器端會話 (session) 的概念??蛻舳嗽谶B接到服務器后,就由 web 服務器產生并維護一個客戶端的會話;當客戶端通過無狀態 http 協議再次連接到服務器時,服務器根據客戶端提交的某種憑據,如 cookie 或 url 參數,將客戶關聯到某個會話上。這種思路在各種開發語言和開發環境中大量得到應用。
在 asp.net 中,web 應用程序和會話狀態被分別進行維護,通過 httpapplication 和 httpsessionstate 分離 web 應用程序與會話的功能。應用程序層邏輯在 global.asax 文件中實現,運行時編譯成 system.web.httpapplication 的實例;會話則作為單獨的 system.web.sessionstate.httpsessionstate 實例,由服務器統一為每個用戶會話維護,通過 asp.net 頁面編譯成的 system.web.ui.page 對象子類的 session 屬性訪問。關于 asp.net 中不同層次關系可參考我以前的一篇文章《.net 1.1中預編譯asp.net頁面實現原理淺析 [1] 自動預編譯機制淺析》,以下簡稱【文1】。
asp.net 在處理客戶端請求時,首先將根據客戶端環境,生成一個 system.web.httpcontext 對象,并將此對象作為執行上下文傳遞給后面的頁面執行代碼。
在【文1】的分析中我們可以看到,httpruntime 在處理頁面請求之前,根據 httpworkerrequest 中給出的環境,構造 httpcontext 對象,并以次對象作為參數從應用程序池中獲取可用應用程序。簡要代碼如下:
以下內容為程序代碼:
private void httpruntime.processrequestinternal(httpworkerrequest wr)
{
// 構造 http 調用上下文對象
httpcontext ctxt = new httpcontext(wr, 0);
//...
// 獲取當前 web 應用程序實例
ihttphandler handler = httpapplicationfactory.getapplicationinstance(ctxt);
// 調用 handler 實際處理頁面請求
}
httpapplicationfactory 工廠內部維護了一個可用的應用程序實例緩沖池,用戶降低應用程序對象構造的負荷。
如果池中沒有可用的應用程序對象實例,此對象工廠最終會調用 system.web.httpruntime.createnonpublicinstance 方法構造新的應用程序實例,并調用其 initinternal 方法初始化。詳細步驟分析見【文1】
以下內容為程序代碼:
internal static ihttphandler httpapplicationfactory.getapplicationinstance(httpcontext ctxt)
{
// 處理定制應用程序
//...
// 處理調試請求
//...
// 判斷是否需要初始化當前 httpapplicationfactory 實例
//...
// 獲取 web 應用程序實例
return httpapplicationfactory._theapplicationfactory.getnormalapplicationinstance(ctxt);
}
private httpapplication httpapplicationfactory.getnormalapplicationinstance(httpcontext context)
{
httpapplication app = null;
// 嘗試從已施放的 web 應用程序實例隊列中獲取
//...
if(app == null)
{
// 構造新的 web 應用程序實例
app = (httpapplication)system.web.httpruntime.createnonpublicinstance(this._theapplicationtype);
// 初始化 web 應用程序實例
app.initinternal(context, this._state, this._eventhandlermethods);
}
return app;
}
這里的 system.web.httpapplication.initinternal 函數完成對應用程序對象的初始化工作,包括調用 httpapplication.initmodules 函數初始化 http 模塊(后面將詳細介紹),并將作為參數傳入的 httpcontext 實例保存到 httpapplication._context 字段中。而此 http 上下文對象將被后面用于獲取會話對象。
以下內容為程序代碼:
public class httpapplication : ...
{
private httpcontext _context;
private httpsessionstate _session;
public httpsessionstate session
{
get
{
httpsessionstate state = null;
if (this._session != null)
{
state = this._session;
}
else if (this._context != null)
{
state = this._context.session;
}
if (state == null)
{
throw new httpexception(httpruntime.formatresourcestring("session_not_available"[img]/images/wink.gif[/img]);
}
return state;
}
}
}
而在 asp.net 頁面中獲取會話的方法也是類似,都是通過 httpcontext 來完成的。
以下內容為程序代碼:
public class page : ...
{
private httpsessionstate _session;
private bool _sessionretrieved;
public virtual httpsessionstate session
{
get
{
if (!this._sessionretrieved)
{
this._sessionretrieved = true;
try
{
this._session = this.context.session;
}
catch (exception)
{
}
}
if (this._session == null)
{
throw new httpexception(httpruntime.formatresourcestring("session_not_enabled"[img]/images/wink.gif[/img]);
}
return this._session;
}
}
}
在 httpcontext 中,實際上是通過一個哈希表保存諸如會話對象之類信息的
以下內容為程序代碼:
public sealed class httpcontext : ...
{
private hashtable _items;
public idictionary items
{
get
{
if (this._items == null)
{
this._items = new hashtable();
}
return this._items;
}
}
public httpsessionstate session
{
get
{
return ((httpsessionstate) this.items["aspsession"]);
}
}
}
而 httpcontext.session 所訪問的又是哪兒來的呢?這就又需要回到我們前面提及的 httpapplication.initmodules 函數。
在 .net 安裝目錄 config 子目錄下的 machine.config 定義了全局性的配置信息,而 httpapplication 就是使用其中 system.web 一節的配置信息進行初始化的。
以下內容為程序代碼:
?。約ystem.web>
<httpmodules>
<add name="outputcache" type="system.web.caching.outputcachemodule" />
?。糰dd name="session" type="system.web.sessionstate.sessionstatemodule" />
?。糰dd name="windowsauthentication" type="system.web.security.windowsauthenticationmodule" />
?。糰dd name="formsauthentication" type="system.web.security.formsauthenticationmodule" />
?。糰dd name="passportauthentication" type="system.web.security.passportauthenticationmodule" />
?。糰dd name="urlauthorization" type="system.web.security.urlauthorizationmodule" />
<add name="fileauthorization" type="system.web.security.fileauthorizationmodule" />
<add name="errorhandlermodule" type="system.web.mobile.errorhandlermodule, system.web.mobile, version=1.0.5000.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a" />
</httpmodules>
</system.web>
httpmodules 節點指定了 httpapplication 需要初始化的模塊列表,而在前面提到的 httpapplication.initmodules 函數正式根據此列表進行初始化的
以下內容為程序代碼:
private void httpapplication.initmodules()
{
httpmodulesconfiguration cfgmodules = ((httpmodulesconfiguration) httpcontext.getappconfig("system.web/httpmodules"[img]/images/wink.gif[/img]);
if (cfgmodules == null)
{
throw new httpexception(httpruntime.formatresourcestring("missing_modules_config"[img]/images/wink.gif[/img]);
}
_modulecollection = cfgmodules.createmodules();
for(int i = 0; i < _modulecollection.count; i++)
{
_modulecollection[i].init(this);
}
globalizationconfig cfgglobal = ((globalizationconfig) httpcontext.getappconfig("system.web/globalization"[img]/images/wink.gif[/img]);
if (cfgglobal != null)
{
_applevelculture = cfgglobal.culture;
_appleveluiculture = cfgglobal.uiculture;
}
}
session 節點對于的 system.web.sessionstate.sessionstatemodule 對象將被 httpmodulesconfiguration.createmodules 方法構造,并調用其 init 函數初始化。sessionstatemodule 類實際上就是負責管理并創建會話,用戶完全可以自行創建一個實現 ihttpmodule 接口的類,實現會話的控制,如實現支持集群的狀態同步等等。
sessionstatemodule.init 方法主要負責 machine.config 文件中的 ses
新聞熱點
疑難解答
圖片精選