共用方式為


IHttpSessionState 介面

定義

定義合約以實作自訂工作階段狀態容器。

public interface class IHttpSessionState
public interface IHttpSessionState
type IHttpSessionState = interface
Public Interface IHttpSessionState
衍生

範例

下列程式代碼範例會實作 IHttpSessionState 介面,以建立名為 MySessionState的新會話狀態容器類別。

using System;
using System.Web;
using System.Web.SessionState;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using System.Threading;
using System.Globalization;

namespace Samples.AspNet.SessionState
{
  public sealed class MySessionState : IHttpSessionState
  {
    const int MAX_TIMEOUT = 24 * 60;  // Timeout cannot exceed 24 hours.

    string                      pId;
    ISessionStateItemCollection pSessionItems;
    HttpStaticObjectsCollection pStaticObjects;
    int                         pTimeout;
    bool                        pNewSession;
    HttpCookieMode              pCookieMode;
    SessionStateMode            pMode;
    bool                        pAbandon;
    bool                        pIsReadonly;

    public MySessionState(string                      id, 
                          ISessionStateItemCollection sessionItems,
                          HttpStaticObjectsCollection staticObjects,
                          int                         timeout,
                          bool                        newSession,
                          HttpCookieMode              cookieMode,
                          SessionStateMode            mode,
                          bool                        isReadonly)
    {
      pId            = id;   
      pSessionItems  = sessionItems;
      pStaticObjects = staticObjects;
      pTimeout       = timeout;    
      pNewSession    = newSession; 
      pCookieMode    = cookieMode;
      pMode          = mode;
      pIsReadonly    = isReadonly;
    }

    public int Timeout
    {
      get { return pTimeout; }
      set
      {
        if (value <= 0)
          throw new ArgumentException("Timeout value must be greater than zero.");

        if (value > MAX_TIMEOUT)
          throw new ArgumentException("Timout cannot be greater than " + MAX_TIMEOUT.ToString());

        pTimeout = value;
      }
    }

    public string SessionID
    {
      get { return pId; }
    }

    public bool IsNewSession
    {
      get { return pNewSession; }
    }

    public SessionStateMode Mode
    {
      get { return pMode; }
    }

    public bool IsCookieless
    {
      get { return CookieMode == HttpCookieMode.UseUri; }
    }

    public HttpCookieMode CookieMode
    {
      get { return pCookieMode; }
    }

    //
    // Abandon marks the session as abandoned. The IsAbandoned property is used by the
    // session state module to perform the abandon work during the ReleaseRequestState event.
    //
    public void Abandon()
    {
      pAbandon = true;
    }

    public bool IsAbandoned
    {
      get { return pAbandon; }
    }

    //
    // Session.LCID exists only to support legacy ASP compatibility. ASP.NET developers should use
    // Page.LCID instead.
    //
    public int LCID
    {
      get { return Thread.CurrentThread.CurrentCulture.LCID; }
      set { Thread.CurrentThread.CurrentCulture = CultureInfo.ReadOnly(new CultureInfo(value)); }
    }

    //
    // Session.CodePage exists only to support legacy ASP compatibility. ASP.NET developers should use
    // Response.ContentEncoding instead.
    //
    public int CodePage
    {
      get
      { 
        if (HttpContext.Current != null)
          return HttpContext.Current.Response.ContentEncoding.CodePage;
        else
          return Encoding.Default.CodePage;
      }
      set
      { 
        if (HttpContext.Current != null)
          HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding(value);
      }
    }

    public HttpStaticObjectsCollection StaticObjects
    {
      get { return pStaticObjects; }
    }

    public object this[string name]
    {
      get { return pSessionItems[name]; }
      set { pSessionItems[name] = value; }
    }

    public object this[int index]
    {
      get { return pSessionItems[index]; }
      set { pSessionItems[index] = value; }
    }

    public void Add(string name, object value)
    {
      pSessionItems[name] = value;        
    }

    public void Remove(string name)
    {
      pSessionItems.Remove(name);
    }

    public void RemoveAt(int index)
    {
      pSessionItems.RemoveAt(index);
    }

    public void Clear()
    {
      pSessionItems.Clear();
    }

    public void RemoveAll()
    {
        Clear();
    }

    public int Count
    {
      get { return pSessionItems.Count; }
    }

    public NameObjectCollectionBase.KeysCollection Keys
    {
      get { return pSessionItems.Keys; }
    }

    public IEnumerator GetEnumerator()
    {
      return pSessionItems.GetEnumerator();
    }

    public void CopyTo(Array items, int index)
    {
      foreach (object o in items)
        items.SetValue(o, index++);
    }

    public object SyncRoot
    {
        get { return this; }
    }

    public bool IsReadOnly
    {
      get { return pIsReadonly; }
    }

    public bool IsSynchronized
    {
      get { return false; }
    }
  }
}
Imports System.Web
Imports System.Web.SessionState
Imports System.Collections
Imports System.Collections.Specialized
Imports System.Text
Imports System.Threading
Imports System.Globalization

Namespace Samples.AspNet.SessionState

  Public NotInheritable Class MySessionState
    Implements IHttpSessionState
  
    Const MAX_TIMEOUT As Integer = 24 * 60  ' Timeout cannot exceed 24 hours.

    Dim pId            As String
    Dim pSessionItems  As ISessionStateItemCollection
    Dim pStaticObjects As HttpStaticObjectsCollection
    Dim pTimeout       As Integer
    Dim pNewSession    As Boolean
    Dim pCookieMode    As HttpCookieMode
    Dim pMode          As SessionStateMode
    Dim pAbandon       As Boolean
    Dim pIsReadonly    As Boolean

    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)
    
      pId            = id   
      pSessionItems  = sessionItems
      pStaticObjects = staticObjects
      pTimeout       = timeout    
      pNewSession    = newSession 
      pCookieMode    = cookieMode
      pMode          = mode
      pIsReadonly    = isReadonly
    End Sub


    Public Property Timeout As Integer Implements IHttpSessionState.Timeout
      Get
        Return pTimeout
      End Get
      Set
        If value <= 0 Then _
          Throw New ArgumentException("Timeout value must be greater than zero.")

        If value > MAX_TIMEOUT Then _
          Throw New ArgumentException("Timout cannot be greater than " & MAX_TIMEOUT.ToString())

        pTimeout = value
      End Set
    End Property


    Public ReadOnly Property SessionID As String Implements IHttpSessionState.SessionID
      Get
        Return pId
      End Get
    End Property


    Public ReadOnly Property IsNewSession As Boolean Implements IHttpSessionState.IsNewSession
      Get
        Return pNewSession
      End Get
    End Property


    Public ReadOnly Property Mode As SessionStateMode Implements IHttpSessionState.Mode    
      Get
        Return pMode
      End Get
    End Property


    Public ReadOnly Property IsCookieless As Boolean Implements IHttpSessionState.IsCookieLess    
      Get
        Return CookieMode = HttpCookieMode.UseUri
      End Get
    End Property


    Public ReadOnly Property CookieMode As HttpCookieMode Implements IHttpSessionState.CookieMode    
      Get
        Return pCookieMode
      End Get
    End Property


    '
    ' Abandon marks the session as abandoned. The IsAbandoned property is used by the
    ' session state module to perform the abandon work during the ReleaseRequestState event.
    '
    Public Sub Abandon() Implements IHttpSessionState.Abandon
      pAbandon = True
    End Sub

    Public ReadOnly Property IsAbandoned As Boolean  
      Get
        Return pAbandon
      End Get
    End Property

    '
        ' Session.LCID exists only to support legacy ASP compatibility. ASP.NET developers should use
    ' Page.LCID instead.
    '
    Public Property LCID As Integer Implements IHttpSessionState.LCID
      Get
        Return Thread.CurrentThread.CurrentCulture.LCID
      End Get
      Set
        Thread.CurrentThread.CurrentCulture = CultureInfo.ReadOnly(new CultureInfo(value))
      End Set
    End Property


    '
        ' Session.CodePage exists only to support legacy ASP compatibility. ASP.NET developers should use
    ' Response.ContentEncoding instead.
    '
    Public Property CodePage As Integer Implements IHttpSessionState.CodePage    
      Get
        If Not HttpContext.Current Is Nothing Then
          Return HttpContext.Current.Response.ContentEncoding.CodePage
        Else
          Return Encoding.Default.CodePage
        End If
      End Get
      Set       
        If Not HttpContext.Current Is Nothing Then _
          HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding(value)
      End Set
    End Property


    Public ReadOnly Property StaticObjects As HttpStaticObjectsCollection _
      Implements IHttpSessionState.StaticObjects
    
      Get
        Return pStaticObjects
      End Get
    End Property


    Public Property Item(name As String) As Object Implements IHttpSessionState.Item
      Get
        Return pSessionItems(name)
      End Get
      Set
        pSessionItems(name) = value
      End Set
    End Property


    Public Property Item(index As Integer) As Object Implements IHttpSessionState.Item    
      Get
        Return pSessionItems(index)
      End Get
      Set
        pSessionItems(index) = value
      End Set
    End Property
        

    Public Sub Add(name As String, value As Object) Implements IHttpSessionState.Add    
      pSessionItems(name) = value        
    End Sub


    Public Sub Remove(name As String) Implements IHttpSessionState.Remove    
      pSessionItems.Remove(name)
    End Sub


    Public Sub RemoveAt(index As Integer) Implements IHttpSessionState.RemoveAt    
      pSessionItems.RemoveAt(index)
    End Sub


    Public Sub Clear() Implements IHttpSessionState.Clear 
      pSessionItems.Clear()
    End Sub

    Public Sub RemoveAll() Implements IHttpSessionState.RemoveAll
        Clear()
    End Sub



    Public ReadOnly Property Count As Integer Implements IHttpSessionState.Count    
      Get
        Return pSessionItems.Count
      End Get
    End Property



    Public ReadOnly Property Keys As NameObjectCollectionBase.KeysCollection _
      Implements IHttpSessionState.Keys
    
      Get
        Return pSessionItems.Keys
      End Get
    End Property


    Public Function GetEnumerator() As IEnumerator Implements IHttpSessionState.GetEnumerator
        Return pSessionItems.GetEnumerator()
    End Function


    Public Sub CopyTo(items As Array, index As Integer) Implements IHttpSessionState.CopyTo    
      For Each o As Object In items
        items.SetValue(o, index)
        index += 1
      Next
    End Sub


    Public ReadOnly Property SyncRoot As Object Implements IHttpSessionState.SyncRoot    
        Get
          Return Me
       End Get
    End Property


    Public ReadOnly Property IsReadOnly As Boolean Implements IHttpSessionState.IsReadOnly    
      Get
        Return pIsReadonly
      End Get
    End Property


    Public ReadOnly Property IsSynchronized As Boolean Implements IHttpSessionState.IsSynchronized    
      Get
        Return False
      End Get
    End Property
  End Class
End Namespace

備註

會話狀態容器可讓您存取目前會話的會話狀態值和相關信息。 會話狀態容器中包含的會話資訊會使用 Session 屬性,透過 HttpSessionState 類別向應用程式程式代碼公開。 類別 HttpSessionState 是會話狀態容器的包裝函式類別。

會話狀態容器的 ASP.NET 實作是 HttpSessionStateContainer 類別。 在要求開始時,在 事件期間 AcquireRequestState ,會 SessionStateModule 建立並填 HttpSessionStateContainer 入 物件,並將它指派給目前的 HttpContext。 在要求結束時,在 事件期間ReleaseRequestState,會SessionStateModule從目前HttpContext擷取 HttpSessionStateContainer 物件,並執行任何必要的會話工作,例如將會話值寫入會話存放區,或放棄會話。 如果要求突然終止,例如透過重新導向,則會 SessionStateModule 呼叫 EndRequest 方法來執行相同的清除。

若要建立自定義會話狀態容器,請建立實作 介面的 IHttpSessionState 類別。 如果您要建立自己的自定義會話狀態容器,您也必須將 取代 SessionStateModule 為您自己的自定義模組。 您的自訂模組會建立自訂工作階段狀態容器的實體,並使用 方法將其新增至目前的 HttpContextAddHttpSessionStateToContext 。 類別概觀中包含自定義會話狀態模組的 SessionStateUtility 範例。

屬性

CodePage

取得或設定目前工作階段的字碼頁識別項。

CookieMode

取得值,指出是否針對無 Cookie 工作階段設定應用程式。

Count

取得工作階段狀態項目集合中的項目數目。

IsCookieless

取得值,指出工作階段 ID 是否內嵌於 URL 或儲存於 HTTP Cookie。

IsNewSession

取得值,指出工作階段是否與目前要求一起建立。

IsReadOnly

取得值,指出工作階段是否為唯讀。

IsSynchronized

取得值,指出對工作階段狀態值之集合的存取是否為同步的 (安全執行緒,Thread-Safe)。

Item[Int32]

按照數值索引取得或設定工作階段狀態項目值。

Item[String]

按照名稱取得或設定工作階段狀態項目值。

Keys

取得儲存在工作階段狀態項目集合中所有值的索引鍵集合。

LCID

取得或設定目前工作階段的地區設定識別項 (LCID)。

Mode

取得目前的工作階段狀態模式。

SessionID

取得工作階段的唯一工作階段識別項。

StaticObjects

取得由 ASP.NET 應用程式檔 Global.asax 內 <object Runat="Server" Scope="Session"/> 標記所宣告的物件集合。

SyncRoot

取得物件,這個物件可以用來對工作階段狀態值集合進行同步存取。

Timeout

取得或設定在工作階段狀態提供者終止工作階段前,要求之間允許的逾時期間 (以分鐘為單位)。

方法

Abandon()

結束目前的工作階段。

Add(String, Object)

將新項目加入至工作階段狀態集合。

Clear()

清除工作階段狀態項目集合中的所有值。

CopyTo(Array, Int32)

將工作階段狀態項目值的集合複製到一維陣列,從陣列中指定的索引開始。

GetEnumerator()

傳回列舉值,其可用於讀取目前工作階段中所有的工作階段狀態項目值。

Remove(String)

從工作階段狀態項目集合中刪除項目。

RemoveAll()

清除工作階段狀態項目集合中的所有值。

RemoveAt(Int32)

從工作階段狀態項目集合中刪除指定索引處的項目。

適用於

另請參閱