Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

SessionStateUtility.GetSessionStaticObjects Method

Gets a reference to the static objects collection for the specified context.

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

public static HttpStaticObjectsCollection GetSessionStaticObjects(
	HttpContext context
)

Parameters

context
Type: System.Web.HttpContext

The HttpContext from which to get the static objects collection.

Return Value

Type: System.Web.HttpStaticObjectsCollection
An HttpStaticObjectsCollection collection populated with the StaticObjects property value for the specified HttpContext.

The GetSessionStaticObjects method is used to retrieve the collection of static objects defined in the Global.asax file for the ASP.NET application. A session-state module implementation will supply the returned HttpStaticObjectsCollection collection to the IHttpSessionState implementation instance that is added to the current context using the AddHttpSessionStateToContext method.

A SessionStateStoreProviderBase can also use the GetSessionStaticObjects method when creating a SessionStateStoreData object.

The following code example shows the handler for the AcquireRequestState event in a custom session-state module. The module retrieves existing session information or creates new session information, including the HttpStaticObjectsCollection collection returned from the GetSessionStaticObjects method, and adds 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;

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0
Show:
© 2015 Microsoft