Manage Web service status (2)
In the following example, the appMyServiceUsage status variable is accessed to increase its value. The following code example shows an XML Web service that uses two XML Web Service Methods: ServerUsage and PerSessionServerUage. ServerUsage is a click counter used to access the ServerUsage XML Web Service method, regardless of how the client communicates with the XML Web Service method. For example, if three clients call the ServerUsage XML Web Service method consecutively, the last client receives a return value of 3. PerSessionServiceUsage is a counter used for a special client session. If the three clients continuously access PerSessionServiceUsage, each client receives the same result during the first call.
[C #] <% @ 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 hasnt 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 participant ualr client session has accessed this XML Web service method.", EnableSession = true)] Public int PerSessionServiceUsage (){ // If the XML Web service method hasnt 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 hasnt 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 participant client session has accessed this XML Web service method.", EnableSession: = True)> _ Public Function PerSessionServiceUsage () As Integer If the XML Web service method hasnt 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 |