Export (0) Print
Expand All

SessionStateUtility.AddHttpSessionStateToContext Method

Applies the session data to the context for the current request.

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

public static void AddHttpSessionStateToContext(
	HttpContext context,
	IHttpSessionState container
)

Parameters

context
Type: System.Web.HttpContext

The HttpContext object to which to add the HttpSessionState object.

container
Type: System.Web.SessionState.IHttpSessionState

The IHttpSessionState implementation instance to add to the specified HTTP context.

ExceptionCondition
HttpException

An HttpSessionState object for the current session has already been added to the specified context.

The AddHttpSessionStateToContext method is used by a session-state module to apply session data to the current request. This occurs during the AcquireRequestState event at the beginning of a request. Session data for the current request is either retrieved for an existing session or created for a new session. The session data is then encapsulated in an IHttpSessionState implementation instance, which is passed to the AddHttpSessionStateToContext method along with the current HttpContext. The supplied session data is then made available to application code through the Session property of the current context.

The following code example shows the handler for the AcquireRequestState event in a custom session-state module. The custom module retrieves existing session information or creates new session information and uses the AddHttpSessionStateToContext method to add it to the HttpContext of the current request. This code example is part of a larger example provided for the SessionStateUtility class.

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

ADD
Show:
© 2015 Microsoft