次の方法で共有


ASP.NET を使用して作成した XML Web サービスでの状態管理

XML Web サービスは、その XML Web サービスを実装するクラスが WebService クラスから派生している場合**、**他の ASP.NET アプリケーションと同じ状態管理オプションにアクセスできます。WebService クラスには、Session オブジェクト、Application オブジェクトなどの多くの共通 ASP.NET オブジェクトが含まれています。

Application オブジェクトが、Web アプリケーション内で実行される全コードにアクセスできるデータ格納機構を提供する一方で、Session オブジェクトは、クライアント セッションごとにデータを格納できるようにします。クライアントが Cookie をサポートしている場合は、Cookie によってクライアント セッションを識別できます。Session オブジェクトに格納されたデータを使用できるのは、WebService から派生するクラスに対して、WebMethod 属性の EnableSession プロパティが true に設定されている場合に限ります**。WebService** から派生するクラスは、自動的に Application オブジェクトにアクセスできます。

特定のクライアント セッション固有の状態にアクセスしてその状態を格納するには

  1. XML Web サービスを宣言します。

    <%@ WebService Language="C#" Class="ServerUsage" %>
    [Visual Basic]
    <%@ WebService Language="VB" Class="ServerUsage" %>
    
  2. System.Web.Services 名前空間への参照を追加します。

    using System.Web.Services;
    [Visual Basic]
    Imports System.Web.Services
    
  3. WebService から、XML Web サービスを実装するクラスを派生します。

    public class ServerUsage : WebService 
    [Visual Basic]
    Public Class ServerUsage : Inherits WebService
    
  4. XML Web サービス メソッドを宣言し、WebMethod 属性の EnableSession プロパティを true に設定します。

    [ WebMethod(EnableSession=true) ]
    public int PerSessionServiceUsage()
    [Visual Basic]
    < WebMethod(EnableSession:=True) > _
    Public Function PerSessionServiceUsage() As Integer
    
  5. 状態を Session に格納し、後で取得できるように、その状態の名前を指定します。値 1MyServiceUsage という名前の状態変数に格納する例を次に示します。

    Session["MyServiceUsage"] = 1;
    [Visual Basic]
    Session("MyServiceUsage") = 1
    
  6. Session に格納されている状態変数にアクセスします。

    MyServiceUsage 状態変数にアクセスし、その値をインクリメントする例を次に示します。

    Session["MyServiceUsage"] = ((int) Session["MyServiceUsage"]) + 1;
    [Visual Basic]
    Session("MyServiceUsage") = CInt(Session("MyServiceUsage")) + 1
    

XML Web サービスを管理する Web アプリケーション固有の状態にアクセスしてその状態を格納するには

  1. XML Web サービスを宣言します。

    <%@ WebService Language="C#" Class="ServerUsage" %>
    [Visual Basic]
    <%@ WebService Language="VB" Class="ServerUsage" %>
    
  2. System.Web.Services 名前空間への参照を追加します。

    using System.Web.Services;
    [Visual Basic]
    Imports System.Web.Services
    
  3. WebService から、XML Web サービスを実装するクラスを派生します。

    public class ServerUsage : WebService
    [Visual Basic]
    Public Class ServerUsage : Inherits WebService
    
  4. XML Web サービス メソッドを宣言します。

    [ WebMethod ]
    public int PerSessionServiceUsage()
    [Visual Basic]
    < WebMethod > _
    Public Function PerSessionServiceUsage() As Integer
    
  5. 状態を Application に格納し、後で取得できるように、その状態の名前を指定します。値 1appMyServiceUsage という名前の状態変数に格納する例を次に示します。

    Application["appMyServiceUsage"] = 1;
    [Visual Basic]
    Application("appMyServiceUsage") = 1
    
  6. Application に格納されている状態変数にアクセスします。

    appMyServiceUsage 状態変数にアクセスし、その値をインクリメントする例を次に示します。

    Application["appMyServiceUsage"] =
       ((int) Application["appMyServiceUsage"]) + 1;
    [Visual Basic]
    Application("appMyServiceUsage") = _
       CInt(Application("appMyServiceUsage")) + 1
    

ServerUsage PerSessionServerUage という 2 つの XML Web サービス メソッドを持つ XML Web サービスのコード例を次に示します。ServerUsage XML Web サービス メソッドは、通信先のクライアントに関係なく、アクセスされるたびに値をインクリメントするヒット カウンタです。たとえば、3 つのクライアントによって ServerUsage XML Web サービス メソッドが連続して呼び出された場合、最後のクライアントは戻り値 3 を受け取ります。ただし、PerSessionServiceUsage は特定のクライアント セッション用のヒット カウンタです。3 つのクライアントが連続して PerSessionServiceUsage にアクセスする場合、各クライアントは最初の呼び出しと同じ結果の 1 を受け取ります。

<%@ WebService Language="C#" Class="ServerUsage" %>
using System.Web.Services;

public class ServerUsage : WebService {
   [ WebMethod(Description="Number of times this service has been accessed.") ]
   public int ServiceUsage() {
     // If the XML Web service method hasn't been accessed,
     // initialize it to 1.
     if (Application["appMyServiceUsage"] == null) 
     {
       Application["appMyServiceUsage"] = 1;
     }
     else
     {
     // Increment the usage count.
       Application["appMyServiceUsage"] = ((int) Application["appMyServiceUsage"]) + 1;
     }
     return  (int) Application["appMyServiceUsage"];
   }

   [ WebMethod(Description="Number of times a particualr client session has accessed this XML Web service method.",EnableSession=true) ]
   public int PerSessionServiceUsage() {
     // If the XML Web service method hasn't been accessed, initialize
     // it to 1.
     if (Session["MyServiceUsage"] == null) 
     {
       Session["MyServiceUsage"] = 1;
     }
     else
     {
     // Increment the usage count.
       Session["MyServiceUsage"] = ((int) Session["MyServiceUsage"]) + 1;
     }
     return  (int) Session["MyServiceUsage"];
   }
}
[Visual Basic]
<%@ WebService Language="VB" Class="ServerUsage" %>
Imports System.Web.Services

Public Class ServerUsage
    Inherits WebService
    
<WebMethod(Description := "Number of times this service has been accessed.")> _
    Public Function ServiceUsage() As Integer
        ' If the XML Web service method hasn't been accessed, initialize
        ' it to 1.
        If Application("appMyServiceUsage") Is Nothing Then            Application("appMyServiceUsage") = 1
        Else
            ' Increment the usage count.
            Application("appMyServiceUsage") = _               CInt(Application("appMyServiceUsage")) + 1
        End If
        Return CInt(Application("appMyServiceUsage"))
    End Function    
    
<WebMethod(Description := "Number of times a particular client session has accessed this XML Web service method.", EnableSession := True)> _
    Public Function  PerSessionServiceUsage() As Integer
       ' If the XML Web service method hasn't been accessed,
       ' initialize it to 1.
        If Session("MyServiceUsage") Is Nothing Then            Session("MyServiceUsage") = 1
        Else
            ' Increment the usage count.
           Session("MyServiceUsage") = CInt(Session("MyServiceUsage")) + 1
        End If
        Return CInt(Session("MyServiceUsage"))
    End Function
    
End Class

参照

ASP.NET の状態管理 | ASP.NET を使用した XML Web サービスの作成