方法 : ASP.NET を使用して作成した Web サービスで状態を管理する
このトピックの対象は、レガシ テクノロジに特定されています。XML Web サービスと XML Web サービス クライアントは以下を使用して作成してください。 Windows Communication Foundation.
Web サービスは、その Web サービスを実装するクラスが WebService クラスから派生している場合、他の ASP.NET アプリケーションと同じ状態管理オプションにアクセスできます。WebService クラスには、Session オブジェクトや Application オブジェクトなどの多くの共通 ASP.NET オブジェクトが含まれています。
特定のクライアント セッション固有の状態にアクセスしてその状態を格納するには
Web サービスを宣言します。
<%@ WebService Language="C#" Class="ServerUsage" %>
<%@ WebService Language="VB" Class="ServerUsage" %>
System.Web.Services 名前空間への参照を追加します。
using System.Web.Services;
Imports System.Web.Services
WebService から、Web サービスを実装するクラスを派生させます。
public class ServerUsage : WebService
Public Class ServerUsage : Inherits WebService
Web サービス メソッドを宣言して、WebMethod 属性の EnableSession プロパティを true に設定します。
[ WebMethod(EnableSession=true) ] public int PerSessionServiceUsage()
< WebMethod(EnableSession:=True) > _ Public Function PerSessionServiceUsage() As Integer
状態を Session に格納し、後で取得できるように、その状態の名前を指定します。値
1
をMyServiceUsage
という名前の状態変数に格納する例を次に示します。Session["MyServiceUsage"] = 1;
Session("MyServiceUsage") = 1
Session に格納されている状態変数にアクセスします。
MyServiceUsage
状態変数にアクセスし、その値をインクリメントする例を次に示します。Session["MyServiceUsage"] = ((int) Session["MyServiceUsage"]) + 1;
Session("MyServiceUsage") = CInt(Session("MyServiceUsage")) + 1
Web サービスをホストする Web アプリケーションに固有の状態にアクセスしてその状態を格納するには
Web サービスを宣言します。
<%@ WebService Language="C#" Class="ServerUsage" %>
<%@ WebService Language="VB" Class="ServerUsage" %>
System.Web.Services 名前空間への参照を追加します。
using System.Web.Services;
Imports System.Web.Services
WebService から、Web サービスを実装するクラスを派生させます。
public class ServerUsage : WebService
Public Class ServerUsage : Inherits WebService
Web サービス メソッドを宣言します。
[ WebMethod ] public int PerSessionServiceUsage()
< WebMethod > _ Public Function PerSessionServiceUsage() As Integer
状態を Application に格納し、後で取得できるように、その状態の名前を指定します。値
1
をappMyServiceUsage
という名前の状態変数に格納する例を次に示します。Application["appMyServiceUsage"] = 1;
Application("appMyServiceUsage") = 1
Application に格納されている状態変数にアクセスします。
appMyServiceUsage
状態変数にアクセスし、その値をインクリメントする例を次に示します。Application["appMyServiceUsage"] = ((int) Application["appMyServiceUsage"]) + 1;
Application("appMyServiceUsage") = _ CInt(Application("appMyServiceUsage")) + 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 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 particular client session has accessed this Web service method.",EnableSession=true) ]
public int PerSessionServiceUsage() {
// If the 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"];
}
}
<%@ 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 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 Web service method.", EnableSession := True)> _
Public Function PerSessionServiceUsage() As Integer
' If the 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