SessionStateStoreProviderBase類
為session-state資料存放區的提供者定義需要成員.
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public abstract class SessionStateStoreProviderBase : ProviderBase
Asp.net session state 使用session-state儲存提供者(provider)從資料存放區讀和寫session資料.一個session-state store provider是繼承SessionStateStoreProviderBass抽象類別和重寫它的成員實現詳細資料存放區.這個Session- state store provider被SessionStateModule類調用在ASP.NET頁面的進程與儲存到資料存放區中進行交流期間並且找回session變數和 相關session資訊如time-out值.
每一個ASP.NET應用程式內Session data被儲存在單獨的SessionID屬性中.ASP.NET應用程式不能分享session data.
你能指定一個自訂的SessionStateStoreProviderBase,通過在設定檔中sessionState元素設定mode屬性為Custom和customProvider屬性指定provide名來實現ASP.NET應用程式.
Locking Session Store Data
因為ASP.NET應用程式是多線程支援響應並發請求,它能並發請求嘗試訪問同一個Session資訊.考慮一個情境有一個frameset有多個 frames訪問同一個應用程式.這裡在服務端並發不同的線程獨立請求在frameset中每一個frame.如果ASP.NET頁面為每一個 frame來源訪問session-state變數,之後,你用多線程並發訪問session 儲存.
在session儲存和異常的session-state行為中避免資料衝突.SessionStateModule和 SessionStateStoreProviderBase類包括了lock功能,就是在ASP.NET頁面在執行的整個期間使用排它瑣瑣住 session 儲存的item.注意如果EnableSessionState屬性設定為ReadOnly,沒有瑣住這個session-store item,另外同一個應用程式ASP.NET頁面能寫一個 session 儲存,所以一個請求read-only session資料仍然需要等待被鎖資料釋放.
在請求開始時:SessionStateModule 對象調用GetItemExclusive方法,在這個期間有一個AcquireRequestState事件,當EnableSessionState 屬性設定為true時,也是預設情形,如果EnableSessionState屬性設定為ReadOnly,SessionStateModule對象 調用的方法就改為GetItem方法.
GetItemExclusive返回一個SessionStateStoreData對象主要是從資料存放區中移植session資訊,更新儲存資料的有 效日期,並且在請求期間在儲存資料中lock session-item data.如果在資料存放區中沒有session-item資料,這時GetItemExclusive方法設定locked參數為false並且返回 null引用.這個原因SessionStateModule對象會在資料存放區中調用CreateNewStoreData方法去建立一個新的 session item.如果session-item資料被找到但資料被lock,這時GetItemExclusive方法設定locked參數為"true",設 置lockAge參數為當前日期和時間減去這時的日期和當item被鎖時的時間(從資料存放區中找回),設定lockId參數為從
資料存放區中找回的lock標記,並且返回一個null引用.這個原因是SessionStateModule對象在每間隔半秒就會常識找回session -item資訊並且獲得關於資料的一個lock.如果lcokAge參數值設定為超越ExecutionTimeout,然後 SessionStateModule 調用ReleaseItemExculusive方法去清出這個lock session-item 資料,並且然後再調用GetItemExclusive方法.
因為被鎖著的session-store資料要等待在每一個線程調用ReleaseItemExclusive的方法被釋放之前要為當前響應,試圖能設定 和釋放已經被其他session釋放和修改的session-state儲存資料調用SetAndReleaseItemExclusive方法.為避免 這種情形,這裡的GetItem和GetItemExclusive方法返回一個lock標記.這個lock標記必須包括每一個請求被修改locked session-store data.Session-store資料只在如果lock標識在資料存放區匹配SesssionStateModule裡提供的lock 標識時會被修改.
Deleting Expired Session Store Data
但為一個session調用Abandon方法時,這個資料要從資料存放區中刪除就要調用RemoverItem方法.另一方面這個資料將保留在 Session資料存放區中直到Sever端在未來再次請求Session時.它是使用SessionStateStoreProviderBase實現刪 除到期session資料.
<configuration>
<connectionStrings>
<add name="OdbcSessionServices" connectionString="DSN=SessionState;" />
</connectionStrings>
<system.web>
<sessionState
mode="Custom"
customProvider="OdbcSessionProvider">
<providers>
<add name="OdbcSessionProvider"
type="Samples.AspNet.Session.OdbcSessionStateStore"
connectionStringName="OdbcSessionServices" />
</providers>
</sessionState>
</system.web>
</configuration>