WebMethodAttribute.EnableSession プロパティ

定義

XML Web サービス メソッドに対してセッション状態が有効かどうかを示します。

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 サービス メソッドに対してセッション状態が有効である場合は 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 Forms クライアントです。 クライアントは、セッションをクライアントのセッション状態に格納することで、セッションを一意に識別する 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 サービスが継承WebServiceWebMethodAttribute、XML Web サービス メソッドに適用する必要があります。このプロパティを EnableSession true. XML Web サービス メソッドにセッション状態が必要ない場合は、無効にするとパフォーマンスが向上する可能性があります。

XML Web サービス クライアントは、XML Web サービスによって返される HTTP Cookie によって一意に識別されます。 XML Web サービスがクライアントのセッション状態を維持するには、クライアントが Cookie を保持する必要があります。 クライアントは、新しいインスタンスを作成し、そのインスタンスをプロキシ クラスの CookieContainer プロパティに CookieContainer 割り当ててから、XML Web サービス メソッドを呼び出すことで HTTP Cookie を受け取ることができます。 プロキシ クラス インスタンスがスコープ外になったときを超えてセッション状態を維持する必要がある場合、クライアントは XML Web サービスへの呼び出しの間に HTTP Cookie を保持する必要があります。 たとえば、Web Forms クライアントは、独自のセッション状態で保存することで HTTP Cookie をCookieContainer保持できます。 すべての XML Web サービスがセッション状態を使用するわけではないため、クライアントは常にクライアント プロキシのプロパティを CookieContainer 使用する必要がないため、XML Web サービスのドキュメントではセッション状態が使用されているかどうかを示す必要があります。

適用対象

こちらもご覧ください