IHttpSessionState Schnittstelle

Definition

Definiert den Vertrag für die Implementierung eines benutzerdefinierten Sitzungszustandscontainers.

public interface class IHttpSessionState
public interface IHttpSessionState
type IHttpSessionState = interface
Public Interface IHttpSessionState
Abgeleitet

Beispiele

Im folgenden Codebeispiel wird die IHttpSessionState -Schnittstelle implementiert, um eine neue Sitzungszustandscontainerklasse namens MySessionStatezu erstellen.

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

Hinweise

Ein Sitzungszustandscontainer bietet Zugriff auf die Sitzungszustandswerte und zugehörige Informationen für die aktuelle Sitzung. Sitzungsinformationen, die in einem Sitzungszustandscontainer enthalten sind, werden dem Anwendungscode über die HttpSessionState -Klasse mit der Session -Eigenschaft verfügbar gemacht. Die HttpSessionState -Klasse ist eine Wrapperklasse für einen Sitzungszustandscontainer.

Die ASP.NET Implementierung eines Sitzungszustandscontainers ist die HttpSessionStateContainer -Klasse. Am Anfang einer Anforderung erstellt und füllt während SessionStateModule des AcquireRequestState Ereignisses ein HttpSessionStateContainer -Objekt und weist es dem aktuellen HttpContextzu. Am Ende einer Anforderung ruft während des ReleaseRequestState Ereignisses SessionStateModule das -Objekt aus dem HttpSessionStateContainer aktuellen HttpContext ab und führt alle erforderlichen Sitzungsvorgänge aus, z. B. das Schreiben der Sitzungswerte in den Sitzungsspeicher oder das Abbrechen der Sitzung. Wenn die Anforderung abrupt beendet wird, z. B. durch eine Umleitung, führt die SessionStateModule gleiche Bereinigung durch Aufrufen der EndRequest -Methode aus.

Um einen benutzerdefinierten Sitzungszustandscontainer zu erstellen, erstellen Sie eine Klasse, die die IHttpSessionState -Schnittstelle implementiert. Wenn Sie Einen eigenen benutzerdefinierten Sitzungszustandscontainer erstellen, müssen Sie auch den SessionStateModule durch Ihr eigenes benutzerdefiniertes Modul ersetzen. Ihr benutzerdefiniertes Modul erstellt eine instance Ihres benutzerdefinierten Sitzungszustandscontainers und fügt ihn dem aktuellen HttpContext mithilfe der AddHttpSessionStateToContext -Methode hinzu. Ein Beispiel für ein benutzerdefiniertes Sitzungszustandsmodul ist in der SessionStateUtility Klassenübersicht enthalten.

Eigenschaften

CodePage

Ruft den Codepagebezeichner der aktuellen Sitzung ab oder legt diesen fest.

CookieMode

Ruft einen Wert ab, der angibt, ob die Anwendung für Sitzungen ohne Cookies konfiguriert ist.

Count

Ruft die Anzahl der Elemente in der Auflistung der Sitzungszustandselemente ab.

IsCookieless

Ruft einen Wert ab, der angibt, ob die Sitzungs-ID in der URL eingebettet oder in einem HTTP-Cookie gespeichert ist.

IsNewSession

Ruft einen Wert ab, der angibt, ob die Sitzung mit der aktuellen Anforderung erstellt wurde.

IsReadOnly

Ruft einen Wert ab, der angibt, ob die Sitzung schreibgeschützt ist.

IsSynchronized

Ruft einen Wert ab, der angibt, ob der Zugriff auf die Auflistung der Sitzungszustandswerte synchronisiert (threadsicher) ist.

Item[Int32]

Ruft einen Sitzungszustandselementwert über den numerischen Index ab oder legt diesen fest.

Item[String]

Ruft einen Sitzungszustandselementwert über den Namen ab oder legt diesen fest.

Keys

Ruft eine Auflistung der Schlüssel aller Werte ab, die in der Auflistung der Sitzungszustandselemente gespeichert sind.

LCID

Ruft den Gebietsschemabezeichner (Locale Identifier, LCID) der aktuellen Sitzung ab oder legt ihn fest.

Mode

Ruft den aktuellen Sitzungszustandsmodus ab.

SessionID

Ruft die eindeutige Sitzungs-ID für die Sitzung ab.

StaticObjects

Ruft eine Auflistung von Objekten ab, die durch <object Runat="Server" Scope="Session"/>-Tags innerhalb der ASP.NET-Anwendungsdatei Global.asax deklariert werden.

SyncRoot

Ruft ein Objekt ab, mit dem der Zugriff auf die Auflistung von Sitzungszustandswerten synchronisiert werden kann.

Timeout

Ruft das Timeout in Minuten ab, das zwischen Anforderungen zulässig ist, bevor der Sitzungszustandsanbieter die Sitzung beendet, oder legt dieses fest.

Methoden

Abandon()

Beendet die aktuelle Sitzung.

Add(String, Object)

Fügt der Sitzungszustandsauflistung ein neues Element hinzu.

Clear()

Löscht alle Werte aus der Auflistung der Sitzungszustandselemente.

CopyTo(Array, Int32)

Kopiert die Auflistung der Sitzungszustandselementwerte in ein eindimensionales Array ab dem angegebenen Index im Array.

GetEnumerator()

Gibt einen Enumerator zurück, mit dem alle Werte der Sitzungszustandselemente in der aktuellen Sitzung gelesen werden können.

Remove(String)

Löscht ein Element aus der Auflistung der Sitzungszustandselemente.

RemoveAll()

Löscht alle Werte aus der Auflistung der Sitzungszustandselemente.

RemoveAt(Int32)

Löscht ein Element an einem angegebenen Index aus der Auflistung der Sitzungszustandselemente.

Gilt für:

Weitere Informationen