17.5 Managing Web Service State

 <  Day Day Up  >  

You want to save state information during a session between your Web Service and a connecting client.


Technique

By default, the Session object that is used in ASP.NET applications is not created when a client calls a method contained within a Web Service. You can, however, enable the Session object for use by adding the EnableSession parameter to the WebMethod attribute. Once you do so, the Session object is created, if it hasn't already been created by a subsequent Web method call, and it is available for use until the session between your Web Service and a connecting client has finished. Listing 17.5 continues the Lottery Web Service example by creating three methods that hold state information. These methods allow a connecting client to specify an initial minimum and maximum value as well as whether to allow duplicates via the BeginNumberList method. Once this method is called, the client can repeatedly call the NextNumber method, which returns a new randomly generated number within the parameters contained in the Session object.

Listing 17.5 Maintaining State Information in Web Services
 [WebMethod( Description="Begins state enabled random number generation",      EnableSession=true)] public void BeginNumberList( int Min, int Max, bool AllowDuplicates ) {     Session["Min"] = Min;     Session["Max"] = Max;     Session["Duplicates"] = AllowDuplicates;     Session["NumberList"] = new ArrayList(); } [WebMethod( Description="Gets next number for state enabled number generation",      EnableSession=true)] public int NextNumber() {     if( Session["Min"] == null  Session["Max"] == null          Session["Duplicates"] == null  Session["NumberList"] == null )         return -1;     // generate random number     Random rand = new Random( DateTime.Now.Millisecond );     int newNum = rand.Next( (int) Session["Min"], (int) Session["Max"] );     // check if duplicates are allowed     if( ((bool)Session["Duplicates"]) == false )     {         // get current list and check if any numbers still available         ArrayList list = (ArrayList) Session["NumberList"];;         if( list.Count >= (int)Session["Max"]-(int)Session["Min"] ) return -1;         // generate a non-duplicate random number         while( list.Contains( newNum ) == true ) newNum = rand.Next(             (int)Session["Min"], Session["Max"] );         // save in list         list.Add( newNum );     }     return newNum; } [WebMethod( Description="Ends state enabled random number generation",      EnableSession=true)] public void EndNumberList() {     Session.Remove( "Min" );     Session.Remove( "Max" );     Session.Remove( "Duplicates" );     Session.Remove( "NumberList" ); } 

Comments

An ASP.NET Web Service behaves similarly to an ASP.NET Web application in that the class is not guaranteed to be loaded at all times. You cannot add instance data that will be preserved across repeated method invocations. By setting the EnableSession parameter in the WebMethod attribute to true , you in effect are turning on the ability to save state information using the HttpSessionState object defined in the WebService class, which your class derives from. You are can access the Application object at any time without using any attributes or attribute parameters. The Application object is a singleton, which means that one instance is used across all sessions. In other words, placing a value in the Application collection while in the space of one user session makes that value available to all other user sessions. Chapter 16, "ASP.NET," demonstrated this process when the Application collection was used to create an ASP.NET chat room.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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