다음을 통해 공유


SessionStateUtility.AddHttpSessionStateToContext 메서드

정의

현재 요청의 컨텍스트에 세션 데이터를 적용합니다.

public:
 static void AddHttpSessionStateToContext(System::Web::HttpContext ^ context, System::Web::SessionState::IHttpSessionState ^ container);
public static void AddHttpSessionStateToContext(System.Web.HttpContext context, System.Web.SessionState.IHttpSessionState container);
static member AddHttpSessionStateToContext : System.Web.HttpContext * System.Web.SessionState.IHttpSessionState -> unit
Public Shared Sub AddHttpSessionStateToContext (context As HttpContext, container As IHttpSessionState)

매개 변수

context
HttpContext

HttpContext 개체를 추가할 개체입니다HttpSessionState.

container
IHttpSessionState

IHttpSessionState 지정된 HTTP 컨텍스트에 추가할 구현 인스턴스입니다.

예외

HttpSessionState 현재 세션에 대한 개체가 이미 지정된 세션에 추가되었습니다context.

예제

다음 코드 예제에서는 사용자 지정 세션 상태 모듈에서 AcquireRequestState 이벤트에 대 한 처리기를 보여줍니다. 사용자 지정 모듈은 기존 세션 정보를 검색하거나 새 세션 정보를 만들고 메서드를 사용하여 AddHttpSessionStateToContext 현재 요청에 추가 HttpContext 합니다. 이 코드 예제는 클래스에 제공된 더 큰 예제의 SessionStateUtility 일부입니다.

//
// 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;
    '
    ' Event handler for HttpApplication.AcquireRequestState
    '
    Private Sub OnAcquireRequestState(ByVal [source] As Object, ByVal args As EventArgs)
        Dim app As HttpApplication = CType([source], HttpApplication)
        Dim context As HttpContext = app.Context
        Dim isNew As Boolean = False
        Dim sessionID As String
        Dim sessionData As SessionItem = Nothing
        Dim supportSessionIDReissue As Boolean = True

        pSessionIDManager.InitializeRequest(context, False, supportSessionIDReissue)
        sessionID = pSessionIDManager.GetSessionID(context)


        If Not (sessionID Is Nothing) Then
            Try
                pHashtableLock.AcquireReaderLock(Int32.MaxValue)
                sessionData = CType(pSessionItems(sessionID), SessionItem)

                If Not (sessionData Is Nothing) Then
                    sessionData.Expires = DateTime.Now.AddMinutes(pTimeout)
                End If
            Finally
                pHashtableLock.ReleaseReaderLock()
            End Try
        Else
            Dim redirected, cookieAdded As Boolean

            sessionID = pSessionIDManager.CreateSessionID(context)
            pSessionIDManager.SaveSessionID(context, sessionID, redirected, cookieAdded)

            If redirected Then Return
        End If
        If sessionData Is Nothing Then
            ' 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()
            End Try
        End If

        ' 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 Then RaiseEvent Start(Me, EventArgs.Empty)
    End Sub


    '
    ' Event for Session_OnStart event in the Global.asax file.
    '
Public Event Start As EventHandler

설명

AddHttpSessionStateToContext 메서드는 세션 상태 모듈에서 현재 요청에 세션 데이터를 적용하는 데 사용됩니다. 이 문제는 요청이 시작될 때 이벤트 중에 AcquireRequestState 발생합니다. 현재 요청에 대한 세션 데이터는 기존 세션에 대해 검색되거나 새 세션에 대해 생성됩니다. 세션 데이터는 현재HttpContext와 함께 메서드에 전달 AddHttpSessionStateToContext 되는 구현 인스턴스에 캡슐화 IHttpSessionState 됩니다. 제공된 세션 데이터는 현재 컨텍스트의 속성을 통해 애플리케이션 코드에서 Session 사용할 수 있게 됩니다.

적용 대상