WebMethodAttribute.EnableSession 속성

정의

XML Web services 메서드의 세션 상태 사용 여부를 나타냅니다.

public:
 property bool EnableSession { bool get(); void set(bool value); };
public bool EnableSession { get; set; }
member this.EnableSession : bool with get, set
Public Property EnableSession As Boolean

속성 값

XML Web services 메서드의 세션 상태를 사용하면 true입니다. 기본값은 false입니다.

예제

XML 웹 서비스 메서드를 액세스 하는 지정 된 횟수 만큼 특정 세션에 세션 상태를 사용 하 여 아래 예제 SessionHitCounter합니다.

<%@ WebService Language="C#" Class="Util" %>
 
 using System.Web.Services;
 
 public class Util: WebService {
   [ WebMethod(Description="Per session Hit Counter",EnableSession=true)]
    public int SessionHitCounter() {
       if (Session["HitCounter"] == null) {
          Session["HitCounter"] = 1;
       }
       else {
          Session["HitCounter"] = ((int) Session["HitCounter"]) + 1;
          }
       return ((int) Session["HitCounter"]);
    }   
 }
<%@ WebService Language="VB" Class="Util" %>
 
Imports System.Web.Services

Public Class Util
    Inherits WebService
    
    <WebMethod(Description := "Per session Hit Counter", _
        EnableSession := True)> _
    Public Function SessionHitCounter() As Integer
        
        If Session("HitCounter") Is Nothing Then
            Session("HitCounter") = 1
        Else
            Session("HitCounter") = CInt(Session("HitCounter")) + 1
        End If
        Return CInt(Session("HitCounter"))
    End Function
End Class

다음 코드 예제는 세션 상태를 사용 하는 XML 웹 서비스의 Web Forms 클라이언트. 클라이언트는 클라이언트의 세션 상태에 저장하여 세션을 고유하게 식별하는 HTTP 쿠키를 유지합니다.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>

<html>

    <script runat="server">

        void EnterBtn_Click(Object Src, EventArgs E) 
    {
      // Create a new instance of a proxy class for your XML Web service.
      ServerUsage su = new ServerUsage();
          CookieContainer cookieJar;

      // Check to see if the cookies have already been saved for this session.
      if (Session["CookieJar"] == null) 
        cookieJar= new CookieContainer();
          else
       cookieJar = (CookieContainer) Session["CookieJar"];

        // Assign the CookieContainer to the proxy class.
        su.CookieContainer = cookieJar;

      // Invoke an XML Web service method that uses session state and thus cookies.
      int count = su.PerSessionServiceUsage();         

      // Store the cookies received in the session state for future retrieval by this session.
      Session["CookieJar"] = cookieJar;

          // Populate the text box with the results from the call to the XML Web service method.
          SessionCount.Text = count.ToString();  
        }
         
    </script>
    <body>
       <form runat=server ID="Form1">
           
             Click to bump up the Session Counter.
             <p>
             <asp:button text="Bump Up Counter" Onclick="EnterBtn_Click" runat=server ID="Button1" NAME="Button1"/>
             <p>
             <asp:label id="SessionCount"  runat=server/>
          
       </form>
    </body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>

<html>

    <script runat=server>

        Public Sub EnterBtn_Click(src As Object, E As EventArgs) 

      ' Create a new instance of a proxy class for your XML Web service.
      Dim su As ServerUsage = new ServerUsage()
          Dim cookieJar As CookieContainer

      ' Check to see if the cookies have already been saved for this session.
      If (Session("CookieJar") Is Nothing) 
        cookieJar= new CookieContainer()
          Else
       cookieJar = Session("CookieJar")
      End If
   

        ' Assign the CookieContainer to the proxy class.
        su.CookieContainer = cookieJar

      ' Invoke an XML Web service method that uses session state and thus cookies.
      Dim count As Integer = su.PerSessionServiceUsage()         

      ' Store the cookies received in the session state for future retrieval by this session.
      Session("CookieJar") = cookieJar

          ' Populate the text box with the results from the call to the XML Web service method.
          SessionCount.Text = count.ToString()  
    End Sub
         
    </script>
    <body>
       <form runat=server ID="Form1">
           
             Click to bump up the Session Counter.
             <p>
             <asp:button text="Bump Up Counter" Onclick="EnterBtn_Click" runat=server ID="Button1" NAME="Button1"/>
             <p>
             <asp:label id="SessionCount"  runat=server/>
          
       </form>
    </body>
</html>

설명

세션 상태를 ASP.NET HttpSessionState 개체에 저장하려면 XML 웹 서비스에서 상속 WebMethodAttributeWebService 하고 XML 웹 서비스 메서드에 적용된 를 속성을 true로 설정 EnableSession 해야 합니다. XML 웹 서비스 메서드에 세션 상태가 필요하지 않은 경우 사용하지 않도록 설정하면 성능이 향상될 수 있습니다.

XML 웹 서비스 클라이언트는 XML 웹 서비스에서 반환된 HTTP 쿠키로 고유하게 식별됩니다. XML 웹 서비스가 클라이언트의 세션 상태를 유지하려면 클라이언트가 쿠키를 유지해야 합니다. 클라이언트는 XML 웹 서비스 메서드를 호출하기 전에 의 CookieContainer 새 인스턴스를 만들고 프록시 클래스의 속성에 할당하여 CookieContainer HTTP 쿠키를 받을 수 있습니다. 프록시 클래스 인스턴스가 범위를 벗어나는 경우 세션 상태를 유지해야 하는 경우 클라이언트는 XML 웹 서비스에 대한 호출 간에 HTTP 쿠키를 유지해야 합니다. 예를 들어 Web Forms 클라이언트는 를 자체 세션 상태로 저장 CookieContainer 하여 HTTP 쿠키를 유지할 수 있습니다. 모든 XML 웹 서비스가 세션 상태를 사용하는 것은 아니므로 클라이언트가 항상 클라이언트 프록시의 속성을 사용할 CookieContainer 필요는 없으므로 XML 웹 서비스에 대한 설명서에는 세션 상태가 사용되는지 여부를 명시해야 합니다.

적용 대상

추가 정보