HttpSessionStateContainer 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
建立新的 HttpSessionStateContainer 物件,並使用指定的設定和值加以初始化。
public:
HttpSessionStateContainer(System::String ^ id, System::Web::SessionState::ISessionStateItemCollection ^ sessionItems, System::Web::HttpStaticObjectsCollection ^ staticObjects, int timeout, bool newSession, System::Web::HttpCookieMode cookieMode, System::Web::SessionState::SessionStateMode mode, bool isReadonly);
public HttpSessionStateContainer (string id, System.Web.SessionState.ISessionStateItemCollection sessionItems, System.Web.HttpStaticObjectsCollection staticObjects, int timeout, bool newSession, System.Web.HttpCookieMode cookieMode, System.Web.SessionState.SessionStateMode mode, bool isReadonly);
new System.Web.SessionState.HttpSessionStateContainer : string * System.Web.SessionState.ISessionStateItemCollection * System.Web.HttpStaticObjectsCollection * int * bool * System.Web.HttpCookieMode * System.Web.SessionState.SessionStateMode * bool -> System.Web.SessionState.HttpSessionStateContainer
Public Sub New (id As String, sessionItems As ISessionStateItemCollection, staticObjects As HttpStaticObjectsCollection, timeout As Integer, newSession As Boolean, cookieMode As HttpCookieMode, mode As SessionStateMode, isReadonly As Boolean)
參數
- id
- String
新工作階段的工作階段識別項。 如果為 null
,則會擲回 ArgumentException。
- sessionItems
- ISessionStateItemCollection
ISessionStateItemCollection,包含新工作階段狀態提供者的工作階段值。
- staticObjects
- HttpStaticObjectsCollection
HttpStaticObjectsCollection,指定由 ASP.NET 應用程式檔 Global.asax 內 <object Runat="Server" Scope="Session"/>
標記所宣告的物件。
- timeout
- Int32
在工作階段狀態提供者終止工作階段之前,要求之間所允許的時間長度 (以分鐘為單位)。
- newSession
- Boolean
如果工作階段是與目前要求一起建立,則為 true
,否則為 false
。
- cookieMode
- HttpCookieMode
新工作階段狀態提供者的 CookieMode。
- mode
- SessionStateMode
其中一個 SessionStateMode 值,指定目前的工作階段狀態模式。
- isReadonly
- Boolean
如果工作階段是唯讀,則為 true
,否則為 false
。
例外狀況
id
為 null
。
範例
下列程式代碼範例顯示AcquireRequestState自定義工作階段狀態模組的事件處理程式,此模組會以AddHttpSessionStateToContext新的或現有的工作階段資訊填入新的 HttpSessionStateContainer 物件,並使用 方法將它新增至HttpContext目前要求的 。 如需自定義會話狀態模組的完整程式碼範例,請參閱 SessionStateUtility 類別概觀。
//
// Event handler for HttpApplication.AcquireRequestState
//
private void OnAcquireRequestState(object source, EventArgs args)
{
HttpApplication app = (HttpApplication)source;
HttpContext context = app.Context;
bool isNew = false;
string sessionID;
SessionItem sessionData = null;
bool supportSessionIDReissue = true;
pSessionIDManager.InitializeRequest(context, false, out supportSessionIDReissue);
sessionID = pSessionIDManager.GetSessionID(context);
if (sessionID != null)
{
try
{
pHashtableLock.AcquireReaderLock(Int32.MaxValue);
sessionData = (SessionItem)pSessionItems[sessionID];
if (sessionData != null)
sessionData.Expires = DateTime.Now.AddMinutes(pTimeout);
}
finally
{
pHashtableLock.ReleaseReaderLock();
}
}
else
{
bool redirected, cookieAdded;
sessionID = pSessionIDManager.CreateSessionID(context);
pSessionIDManager.SaveSessionID(context, sessionID, out redirected, out cookieAdded);
if (redirected)
return;
}
if (sessionData == null)
{
// Identify the session as a new session state instance. Create a new SessionItem
// and add it to the local Hashtable.
isNew = true;
sessionData = new SessionItem();
sessionData.Items = new SessionStateItemCollection();
sessionData.StaticObjects = SessionStateUtility.GetSessionStaticObjects(context);
sessionData.Expires = DateTime.Now.AddMinutes(pTimeout);
try
{
pHashtableLock.AcquireWriterLock(Int32.MaxValue);
pSessionItems[sessionID] = sessionData;
}
finally
{
pHashtableLock.ReleaseWriterLock();
}
}
// Add the session data to the current HttpContext.
SessionStateUtility.AddHttpSessionStateToContext(context,
new HttpSessionStateContainer(sessionID,
sessionData.Items,
sessionData.StaticObjects,
pTimeout,
isNew,
pCookieMode,
SessionStateMode.Custom,
false));
// Execute the Session_OnStart event for a new session.
if (isNew && Start != null)
{
Start(this, EventArgs.Empty);
}
}
//
// Event for Session_OnStart event in the Global.asax file.
//
public event EventHandler Start;
'
' Event handler for HttpApplication.AcquireRequestState
'
Private Sub OnAcquireRequestState(ByVal [source] As Object, ByVal args As EventArgs)
Dim app As HttpApplication = CType([source], HttpApplication)
Dim context As HttpContext = app.Context
Dim isNew As Boolean = False
Dim sessionID As String
Dim sessionData As SessionItem = Nothing
Dim supportSessionIDReissue As Boolean = True
pSessionIDManager.InitializeRequest(context, False, supportSessionIDReissue)
sessionID = pSessionIDManager.GetSessionID(context)
If Not (sessionID Is Nothing) Then
Try
pHashtableLock.AcquireReaderLock(Int32.MaxValue)
sessionData = CType(pSessionItems(sessionID), SessionItem)
If Not (sessionData Is Nothing) Then
sessionData.Expires = DateTime.Now.AddMinutes(pTimeout)
End If
Finally
pHashtableLock.ReleaseReaderLock()
End Try
Else
Dim redirected, cookieAdded As Boolean
sessionID = pSessionIDManager.CreateSessionID(context)
pSessionIDManager.SaveSessionID(context, sessionID, redirected, cookieAdded)
If redirected Then Return
End If
If sessionData Is Nothing Then
' Identify the session as a new session state instance. Create a new SessionItem
' and add it to the local Hashtable.
isNew = True
sessionData = New SessionItem()
sessionData.Items = New SessionStateItemCollection()
sessionData.StaticObjects = SessionStateUtility.GetSessionStaticObjects(context)
sessionData.Expires = DateTime.Now.AddMinutes(pTimeout)
Try
pHashtableLock.AcquireWriterLock(Int32.MaxValue)
pSessionItems(sessionID) = sessionData
Finally
pHashtableLock.ReleaseWriterLock()
End Try
End If
' Add the session data to the current HttpContext.
SessionStateUtility.AddHttpSessionStateToContext(context, _
New HttpSessionStateContainer(sessionID, _
sessionData.Items, _
sessionData.StaticObjects, _
pTimeout, _
isNew, _
pCookieMode, _
SessionStateMode.Custom, _
False))
' Execute the Session_OnStart event for a new session.
If isNew Then RaiseEvent Start(Me, EventArgs.Empty)
End Sub
'
' Event for Session_OnStart event in the Global.asax file.
'
Public Event Start As EventHandler