This topic has not yet been rated - Rate this topic

HttpSessionStateContainer Constructor

Creates a new HttpSessionStateContainer object and initializes it with the specified settings and values.

Namespace:  System.Web.SessionState
Assembly:  System.Web (in System.Web.dll)
public HttpSessionStateContainer(
	string id,
	ISessionStateItemCollection sessionItems,
	HttpStaticObjectsCollection staticObjects,
	int timeout,
	bool newSession,
	HttpCookieMode cookieMode,
	SessionStateMode mode,
	bool isReadonly
)

Parameters

id
Type: System.String
A session identifier for the new session. If null, an ArgumentException is thrown.
sessionItems
Type: System.Web.SessionState.ISessionStateItemCollection
An ISessionStateItemCollection that contains the session values for the new session-state provider.
staticObjects
Type: System.Web.HttpStaticObjectsCollection
An HttpStaticObjectsCollection that specifies the objects declared by <object Runat="Server" Scope="Session"/> tags within the ASP.NET application file Global.asax.
timeout
Type: System.Int32
The amount of time, in minutes, allowed between requests before the session-state provider terminates the session.
newSession
Type: System.Boolean
true to indicate the session was created with the current request; otherwise, false.
cookieMode
Type: System.Web.HttpCookieMode
The CookieMode for the new session-state provider.
mode
Type: System.Web.SessionState.SessionStateMode
One of the SessionStateMode values that specifies the current session-state mode.
isReadonly
Type: System.Boolean
true to indicate the session is read-only; otherwise, false.
ExceptionCondition
ArgumentNullException

id is null.

The following code example shows the AcquireRequestState event handler for a custom session-state module that populates a new HttpSessionStateContainer object with new or existing session information and adds it to the HttpContext of the current request using the AddHttpSessionStateToContext method. For a full code example of a custom session-state module, see the SessionStateUtility class overview.


//
// 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;


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)

Community Additions

ADD
© 2013 Microsoft. All rights reserved.