Managing State Information

Team-Fly    

 
Visual Basic .NET Unleashed
By Paul Kimmel
Table of Contents
Chapter 18.  Using and Implementing Web Services

Managing State Information

There are two objects you can use to manage state information. The Application and Session objects are inherited from the WebService class. The Application object is used to manage state information for the entire application. The Session object is used to manage per-session information.

To indicate that a WebMethod will be maintaining session information, we need to initialize the WebMethodAttribute.EnableSession to True. This is accomplished by providing a named argument to the WebMethod attribute. Listing 18.6 demonstrates maintaining roll information for the Dice Web Service created earlier in the chapter.

Listing 18.6 Extends the code from Listing 18.4 to include the sum, roll-count, and average roll of the dice, demonstrating session state information
  1:  Imports System.Web.Services  2:   3:  <WebService(_  4:  Description:="Represents random Dice roll values.", _  5:  Name:="Dice", Namespace:="http://www.softconcepts.com/")> _  6:  Public Class Service1  7:  Inherits System.Web.Services.WebService  8:   9:  [ Web Services Designer Generated Code ]  10:   11:  <WebMethod(_  12:  Description:="Returns random values representing six-sided dice.", _  13:  EnableSession:=True)> _  14:  Public Function Roll(ByVal HowMany As Integer) As Integer()  15:  Dim DiceValues(HowMany) As Integer  16:  Dim R As New Random()  17:   18:  Dim I As Integer  19:  For I = 0 To DiceValues.Length - 1  20:  DiceValues(I) = R.Next(1, 7)  21:  Next  22:   23:  UpdateAverage(DiceValues)  24:   25:  Return DiceValues  26:  End Function  27:   28:  <WebMethod(EnableSession:=True)> _  29:  Public Function GetCount() As Integer  30:  Return Count  31:  End Function  32:   33:  <WebMethod(EnableSession:=True)> _  34:  Public Function GetSum() As Integer  35:  Return Sum  36:  End Function  37:   38:  Private Property Count() As Integer  39:  Get  40:  Return CInt(Session("Count"))  41:  End Get  42:   43:  Set(ByVal Value As Integer)  44:  Session("Count") = Value  45:  End Set  46:   47:  End Property  48:   49:  Private Property Sum() As Integer  50:  Get  51:  Return CInt(Session("Sum"))  52:  End Get  53:  Set(ByVal Value As Integer)  54:  Session("Sum") = Value  55:  End Set  56:  End Property  57:   58:  <WebMethod(EnableSession:=True)> _  59:  Private Sub UpdateAverage(ByVal Values() As Integer)  60:  Count += 1  61:   62:  Dim I As Integer  63:  For I = 0 To Values.Length - 1  64:  Sum += Values(I)  65:  Next  66:   67:  End Sub  68:   69:  <WebMethod(EnableSession:=True)> _  70:  Public Function AverageRoll() As Integer  71:   72:  Try  73:  Return Sum \  Count  74:  Catch  75:  Return 0  76:  End Try  77:  End Function  78:   79:  End Class 

The Roll, GetCount, GetSum, and AverageRoll methods all demonstrate WebMethod procedures that use the EnableSession named argument. The key to storing session information is storing the session state information in the Session object. Session is an instance of the HttpSessionState class, and maintains a collection of session information that you explicitly add to the Session object.

The properties Count and Sumon lines 38 through 56simplify using the Session object by wrapping the storing and retrieving of the session state information. The Session object stores information as plain objects, so we have to convert the value to a specific value when retrieving information back from the Session object, as demonstrated on lines 40 and 51.

The initial values for the two Session variables are provided in the global.asax module, in the Session_Start event. The following fragment demonstrates the implementation of that method.

 Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)   Session("Sum") = 0   Session("Count") = 0 End Sub 

We still have to use the exception handler beginning on line 72 and ending on line 76 to catch the divide-by-zero exception that will occur if the roll count is zero.


Team-Fly    
Top
 


Visual BasicR. NET Unleashed
Visual BasicR. NET Unleashed
ISBN: N/A
EAN: N/A
Year: 2001
Pages: 222

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net