IHttpSessionState Interface

IHttpSessionState Interface

 

Defines the contract to implement a custom session-state container.

Namespace:   System.Web.SessionState
Assembly:  System.Web (in System.Web.dll)

public interface IHttpSessionState

NameDescription
System_CAPS_pubpropertyCodePage

Gets or sets the code-page identifier for the current session.

System_CAPS_pubpropertyCookieMode

Gets a value that indicates whether the application is configured for cookieless sessions.

System_CAPS_pubpropertyCount

Gets the number of items in the session-state item collection.

System_CAPS_pubpropertyIsCookieless

Gets a value indicating whether the session ID is embedded in the URL or stored in an HTTP cookie.

System_CAPS_pubpropertyIsNewSession

Gets a value indicating whether the session was created with the current request.

System_CAPS_pubpropertyIsReadOnly

Gets a value indicating whether the session is read-only.

System_CAPS_pubpropertyIsSynchronized

Gets a value indicating whether access to the collection of session-state values is synchronized (thread safe).

System_CAPS_pubpropertyItem[Int32]

Gets or sets a session-state item value by numerical index.

System_CAPS_pubpropertyItem[String]

Gets or sets a session-state item value by name.

System_CAPS_pubpropertyKeys

Gets a collection of the keys for all values stored in the session-state item collection.

System_CAPS_pubpropertyLCID

Gets or sets the locale identifier (LCID) of the current session.

System_CAPS_pubpropertyMode

Gets the current session-state mode.

System_CAPS_pubpropertySessionID

Gets the unique session identifier for the session.

System_CAPS_pubpropertyStaticObjects

Gets a collection of objects declared by <object Runat="Server" Scope="Session"/> tags within the ASP.NET application file Global.asax.

System_CAPS_pubpropertySyncRoot

Gets an object that can be used to synchronize access to the collection of session-state values.

System_CAPS_pubpropertyTimeout

Gets and sets the time-out period (in minutes) allowed between requests before the session-state provider terminates the session.

NameDescription
System_CAPS_pubmethodAbandon()

Ends the current session.

System_CAPS_pubmethodAdd(String, Object)

Adds a new item to the session-state collection.

System_CAPS_pubmethodClear()

Clears all values from the session-state item collection.

System_CAPS_pubmethodCopyTo(Array, Int32)

Copies the collection of session-state item values to a one-dimensional array, starting at the specified index in the array.

System_CAPS_pubmethodGetEnumerator()

Returns an enumerator that can be used to read all the session-state item values in the current session.

System_CAPS_pubmethodRemove(String)

Deletes an item from the session-state item collection.

System_CAPS_pubmethodRemoveAll()

Clears all values from the session-state item collection.

System_CAPS_pubmethodRemoveAt(Int32)

Deletes an item at a specified index from the session-state item collection.

A session-state container provides access to the session-state values and related information for the current session. Session information included in a session-state container is exposed to application code through the HttpSessionState class using the Session property. The HttpSessionState class is a wrapper class for a session-state container.

The ASP.NET implementation of a session-state container is the HttpSessionStateContainer class. At the beginning of a request, during the AcquireRequestState event, the SessionStateModule creates and populates an HttpSessionStateContainer object and assigns it to the current HttpContext. At the end of a request, during the ReleaseRequestState event, the SessionStateModule retrieves the HttpSessionStateContainer object from the current HttpContext and performs any required session work, such as writing the session values to the session store, or abandoning the session. If the request is abruptly terminated, such as through a redirect, the SessionStateModule performs the same cleanup by calling the EndRequest method.

To create a custom session-state container, create a class that implements the IHttpSessionState interface. If you are creating your own custom session-state container, you must also replace the SessionStateModule with your own custom module. Your custom module will create an instance of your custom session-state container and add it to the current HttpContext using the AddHttpSessionStateToContext method. An example of a custom session-state module is included in the SessionStateUtility class overview.

The following code example implements the IHttpSessionState interface to create a new session-state container class named 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; }
    }
  }
}

.NET Framework
Available since 2.0
Return to top
Show:
© 2016 Microsoft