WebMethodAttribute.EnableSession 属性

指示是否为 XML Web services 方法启用会话状态。

**命名空间:**System.Web.Services
**程序集:**System.Web.Services(在 system.web.services.dll 中)

语法

声明
Public Property EnableSession As Boolean
用法
Dim instance As WebMethodAttribute
Dim value As Boolean

value = instance.EnableSession

instance.EnableSession = value
public bool EnableSession { get; set; }
public:
property bool EnableSession {
    bool get ();
    void set (bool value);
}
/** @property */
public boolean get_EnableSession ()

/** @property */
public void set_EnableSession (boolean value)
public function get EnableSession () : boolean

public function set EnableSession (value : boolean)

属性值

如果为 XML Web services 方法启用会话状态,则为 true。默认为 false

备注

为了将会话状态存储在 ASP.NET HttpSessionState 对象中,XML Web services 必须继承自 WebService 和应用于 XML Web services 方法的 WebMethodAttribute(将 EnableSession 属性 (Property) 设置为 true)。如果 XML Web services 方法不需要会话状态,则禁用它即可提高性能。

XML Web services 客户端由 XML Web services 返回的 HTTP cookie 唯一标识。为了使 XML Web services 维护客户端的会话状态,客户端必须保持 Cookie。客户端可以通过在调用 XML Web services 之前创建 CookieContainer 的新实例并将其分配给代理类的 CookieContainer 属性 (Property) 来接收 HTTP Cookie。若需在代理类实例超出范围时维护会话状态,客户端必须在 XML Web services 调用之间保持 HTTP Cookie。例如,Web 窗体客户端可以通过在自己的会话状态中保存 CookieContainer 来保持 HTTP Cookie。因为并非所有 XML Web services 都使用会话状态,因而并不总是要求客户端使用客户端代理的 CookieContainer 属性 (Property),所以 XML Web services 文档应该说明是否使用会话状态。

示例

下面的示例使用会话状态确定某个特定会话访问 XML Web services 方法 SessionHitCounter 的次数。

<%@ 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
<%@ 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"]);
    }   
 }

下面的代码示例是使用会话状态的 XML Web services 的 Web 窗体客户端。该客户端通过将唯一标识会话的 HTTP cookie 存储在客户端的会话状态中来保持它。

<%@ 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>
<%@ 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>

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

WebMethodAttribute 类
WebMethodAttribute 成员
System.Web.Services 命名空间
CookieContainer