2009-10-23 5 views
2

페이지 메서드를 통해 세션의 값에 액세스하는 데 문제가 있습니다.페이지 방법을 통해 액세스 할 때 세션 문제

아래 예제는 문제를 간단한 형태로 보여줍니다. 페이지에는 3 개의 버튼이 있으며 각각은 다른 페이지 방법을 호출합니다. 그러면 세션에서 동일한 변수가 될 것으로 예상되는 것을 액세스합니다. 실제로 반환되는 값은 아래에 설명 된 것처럼 각 페이지 메서드마다 다릅니다.

웹 서버에 배포 할 때만이 문제가 발생합니다. 로컬에서 테스트 할 때 문제가되지 않습니다.

Example output: 

Method  Session Value 
DoMethod1 597cbe1c-5391-4b93-af6b-0da0068b264b 
DoMethod2 35df48aa-8a58-4008-b353-be7b9f950395 
DoMethod3 35df48aa-8a58-4008-b353-be7b9f950395 
DoMethod1 597cbe1c-5391-4b93-af6b-0da0068b264b 
DoMethod2 597cbe1c-5391-4b93-af6b-0da0068b264b 
DoMethod1 35df48aa-8a58-4008-b353-be7b9f950395 
DoMethod3 35df48aa-8a58-4008-b353-be7b9f950395 
DoMethod1 597cbe1c-5391-4b93-af6b-0da0068b264b 
DoMethod2 597cbe1c-5391-4b93-af6b-0da0068b264b 
DoMethod3 35df48aa-8a58-4008-b353-be7b9f950395 




Imports System.Web.Services 

Public Class MethodResult 
    Public MethodName As String 
    Public SessionID As String 
    Public SessionValue As Object 
End Class 

Partial Public Class sessiontest 
    Inherits System.Web.UI.Page 

    <WebMethod()> _ 
    Public Shared Function DoMethod1() As MethodResult 
     If HttpContext.Current.Session("sharedvalue") Is Nothing Then 
      HttpContext.Current.Session("sharedvalue") = Guid.NewGuid 
     End If 
     Dim ret = New MethodResult 
     ret.MethodName = "DoMethod1" 
     ret.SessionID = HttpContext.Current.Session.SessionID 
     ret.SessionValue = HttpContext.Current.Session("sharedvalue") 
     Return ret 
    End Function 


    <WebMethod()> _ 
    Public Shared Function DoMethod2() As MethodResult 
     If HttpContext.Current.Session("sharedvalue") Is Nothing Then 
      HttpContext.Current.Session("sharedvalue") = Guid.NewGuid 
     End If 
     Dim ret = New MethodResult 
     ret.MethodName = "DoMethod2" 
     ret.SessionID = HttpContext.Current.Session.SessionID 
     ret.SessionValue = HttpContext.Current.Session("sharedvalue") 
     Return ret 

    End Function 


    <WebMethod()> _ 
    Public Shared Function DoMethod3() As MethodResult 
     If HttpContext.Current.Session("sharedvalue") Is Nothing Then 
      HttpContext.Current.Session("sharedvalue") = Guid.NewGuid 
     End If 
     Dim ret = New MethodResult 
     ret.MethodName = "DoMethod3" 
     ret.SessionID = HttpContext.Current.Session.SessionID 
     ret.SessionValue = HttpContext.Current.Session("sharedvalue") 
     Return ret 

    End Function 

End Class 


<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="sessiontest.aspx.vb" Inherits="project1.sessiontest" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" /> 
    <script type="text/javascript"> 
     function method1() { 
      PageMethods.DoMethod1(methodComplete); 
     } 
     function method2() { 
      PageMethods.DoMethod2(methodComplete); 
     } 
     function method3() { 
      PageMethods.DoMethod3(methodComplete); 
     } 
     //complete 
     function methodComplete(result) { 
      document.getElementById('output').innerHTML += "<tr><td>" + result.MethodName + "</td><td>" + result.SessionID + "</td><td>" + result.SessionValue + "</td></tr>"; 
     } 

    </script>  
    <div> 
     <input type="button" value="Method 1" onclick="method1();" id="btn1" /> 
     <br /> 
     <input type="button" value="Method 2" onclick="method2();" id="Button1" /> 
     <br /> 
     <input type="button" value="Method 3" onclick="method3();" id="Button2" /> 
     <br /> 
     <table id="output" style="width:100%;"> 
      <tr> 
       <th>Method Name</th> 
       <th>Session ID</th> 
       <th>Session Value</th> 
      </tr> 
     </table> 
    </div> 
    </form> 
</body> 
</html> 

답변

6

웹 메소드의 세션 상태를 활성화해야합니다.

예 :

<WebMethod(EnableSession:=True)> _ 
Public Shared Function DoMethod1() As MethodResult 
    If HttpContext.Current.Session("sharedvalue") Is Nothing Then 
     HttpContext.Current.Session("sharedvalue") = Guid.NewGuid 
    End If 
    Dim ret = New MethodResult 
    ret.MethodName = "DoMethod1" 
    ret.SessionID = HttpContext.Current.Session.SessionID 
    ret.SessionValue = HttpContext.Current.Session("sharedvalue") 
    Return ret 
End Function 

체크 아웃 자세한 내용은이 msdn article.

+0

감사합니다. – user23048345

관련 문제