次の方法で共有


WebMethodAttribute.EnableSession プロパティ

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

Public Property EnableSession As Boolean
[C#]
public bool EnableSession {get; set;}
[C++]
public: __property bool get_EnableSession();public: __property void set_EnableSession(bool);
[JScript]
public function get EnableSession() : Boolean;public function set EnableSession(Boolean);

プロパティ値

XML Web サービス メソッドに対してセッション状態が有効である場合は true 。既定値は false です。

解説

ASP.NET HttpSessionState オブジェクト内にセッション状態を格納するには、 EnableSession プロパティを true に設定して、XML Web サービスを WebService から継承し、 WebMethodAttribute を、XML Web サービス メソッドに適用する必要があります。XML Web サービス メソッドに対してセッション状態が不要な場合は、セッション状態を無効にすると、パフォーマンスを向上できます。

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

使用例

[Visual Basic, C#] セッション状態を使用して、特定のセッションが XML Web サービス メソッド 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


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

[Visual Basic, C#] セッション状態を使用する XML Web サービスの 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>

[C#] 
<%@ 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>

[C++, JScript] C++ および JScript のサンプルはありません。Visual Basic および C# のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

WebMethodAttribute クラス | WebMethodAttribute メンバ | System.Web.Services 名前空間 | CookieContainer