Sdílet prostřednictvím


WebMethodAttribute.EnableSession Vlastnost

Definice

Určuje, zda je pro metodu webové služby XML povolen stav relace.

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

Hodnota vlastnosti

true pokud je pro metodu webové služby XML povolen stav relace. Výchozí hodnota je false.

Příklady

Následující příklad používá stav relace k určení, kolikrát konkrétní relace přistupuje k metodě SessionHitCounterwebové služby XML .

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

Následující příklad kódu je klient webových formulářů webové služby XML, který používá stav relace. Klient uchovává soubor cookie HTTP, který jednoznačně identifikuje relaci tím, že ji uloží do stavu relace klienta.

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

Poznámky

Aby bylo možné uložit stav relace v objektu ASP.NET HttpSessionState , musí webová služba XML dědit z WebService metody webové služby XML a WebMethodAttribute použít ji pro metodu webové služby XML a nastavit EnableSession vlastnost na true. Pokud není stav relace pro metodu webové služby XML potřeba, může se jeho zakázání zlepšit výkon.

Klient webové služby XML je jednoznačně identifikován souborem cookie HTTP vráceným webovou službou XML. Aby webová služba XML zachovala stav relace pro klienta, musí klient zachovat soubor cookie. Klienti mohou přijímat soubor cookie HTTP vytvořením nové instance CookieContainer a přiřazením této vlastnosti ke CookieContainer třídě proxy před voláním metody webové služby XML. Pokud potřebujete zachovat stav relace nad rámec, když instance třídy proxy přestane být oborem, klient musí zachovat soubor cookie HTTP mezi voláními webové služby XML. Klient webových formulářů může například zachovat soubor cookie HTTP uložením do vlastního CookieContainer stavu relace. Vzhledem k tomu, že ne všechny webové služby XML používají stav relace, a proto klienti nemusí vždy používat CookieContainer vlastnost proxy klienta, měla by dokumentace pro webovou službu XML uvést, zda je použit stav relace.

Platí pro

Viz také