WebMethodAttribute.EnableSession 屬性

定義

指示是否啟用 XML Web Service 方法的工作階段狀態。

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

屬性值

Boolean

如果啟用 XML Web Service 方法的工作階段狀態,則為 true。 預設為 false

範例

下列範例會使用會話狀態來判斷特定會話存取 XML Web 服務方法 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 服務的 Web Form 用戶端。 用戶端會保存可唯一識別會話的 HTTP Cookie,方法是將它儲存在用戶端的會話狀態中。

<%@ 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 Web 服務必須繼承自 WebService ,並 WebMethodAttribute 套用至 XML Web 服務方法,將 EnableSession 屬性設定為 true 。 如果 XML Web 服務方法不需要會話狀態,則停用可能會改善效能。

XML Web 服務用戶端是由 XML Web 服務所傳回的 HTTP Cookie 唯一識別。 為了讓 XML Web 服務維護用戶端的會話狀態,用戶端必須保存 Cookie。 用戶端可以藉由建立 的新實例,並將該實例 CookieContainer 指派給 CookieContainer Proxy 類別的 屬性,再呼叫 XML Web 服務方法,以接收 HTTP Cookie。 如果您需要在 Proxy 類別實例超出範圍時維護超出範圍的會話狀態,用戶端必須在對 XML Web 服務的呼叫之間保存 HTTP Cookie。 例如,Web Form用戶端可以儲存自己的會話狀態來 CookieContainer 保存 HTTP Cookie。 因為並非所有 XML Web 服務都使用會話狀態,因此用戶端不一定需要使用 CookieContainer 用戶端 Proxy 的 屬性,所以 XML Web 服務的檔應該指出是否使用會話狀態。

適用於

另請參閱