WebMethodAttribute.EnableSession Proprietà

Definizione

Indica se lo stato della sessione è attivato per un metodo di servizio Web XML.

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

Valore della proprietà

Boolean

true se lo stato della sessione è attivato per un metodo di servizio Web XML. Il valore predefinito è false.

Esempio

Nell'esempio seguente viene utilizzato lo stato della sessione per determinare il numero di volte in cui una determinata sessione accede al metodo SessionHitCounterdel servizio Web 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

L'esempio di codice seguente è un client Web Forms di un servizio Web XML che utilizza lo stato della sessione. Il client mantiene il cookie HTTP che identifica in modo univoco la sessione archiviandola nello stato di sessione del client.

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

Commenti

Per archiviare lo stato della sessione nell'oggetto ASP.NETHttpSessionState, il servizio Web XML deve ereditare da WebService e un WebMethodAttribute oggetto applicato al metodo del servizio Web XML, impostando la EnableSession proprietà su true. Se lo stato della sessione non è necessario per un metodo di servizio Web XML, la disabilitazione potrebbe migliorare le prestazioni.

Un client del servizio Web XML è identificato in modo univoco da un cookie HTTP restituito da un servizio Web XML. Affinché un servizio Web XML mantenga lo stato della sessione per un client, il client deve rendere persistente il cookie. I client possono ricevere il cookie HTTP creando una nuova istanza di CookieContainer e assegnandola alla CookieContainer proprietà della classe proxy prima di chiamare il metodo del servizio Web XML. Se è necessario mantenere lo stato della sessione oltre quando l'istanza della classe proxy esce dall'ambito, il client deve rendere persistente il cookie HTTP tra le chiamate al servizio Web XML. Ad esempio, un client Web Forms può rendere persistente il cookie HTTP salvando l'oggetto CookieContainer nello stato della sessione. Poiché non tutti i servizi Web XML usano lo stato sessione e pertanto i client non sono sempre necessari per utilizzare la CookieContainer proprietà di un proxy client, la documentazione per il servizio Web XML deve indicare se viene utilizzato lo stato della sessione.

Si applica a

Vedi anche