Global variables in Web Service class

Hi,

I wanted to write a WS that had about 6 Clients.  How can I share a variable between all these clients

What I wanted to do, is have the WS store a Time stamp of the time that the newest data was retreived.

I want to do it like this:


namespace myWebService
{
   public class myWebService : System.Web.Services.WebService
   {
      
private string myName = "Unassigned";
      
private DataSet WaitingDataSet;
      private
DateTime WaitingTimeStamp;

 


If the 1. Client connected and the data would be fetched therefor the time stamp stored and the data.  Could the 2. Client then come in and retreive that time stamp   Or does the 1. Client have its own "instance" of the WS class

Sorry my english is so bad.


Answer this question

Global variables in Web Service class

  • JRC Systems

    Are you sharing the actual value of the variable, or just the variable itself   If it's the variable (you want each client to set it's own timestamp), then do note that when one client modifies the value, every other client's value gets updated too because of the static.

  • Stuart Bray

    Thank you all for your replies.
    I was hoping I could store a TimeStamp on the Server and Each Client would have its TimeStamp.
    Then the Client would pass in its TimeStamp, the Server would look at it compare it to the TimeStamp on the server and if the Client had the same TimeStamp return null.  Else if the Server had a newer TimeStamp return the Data that the server had stored in memory and then also update both TimeStamps and return one to the client.

    Something in this direction.

    Sorry my bad english, trying to make myself understandable.  I am trying to create some sort of a cache logic on our Application Server Module(IIS server).  Perhaps a better place for this logic would be in the Business Components DLL

  • 吴培

    Thank you for the reply.

    I added static in front and that seemed to help me out.


    private static DateTime WaitingTimeStamp;

     


    Am I wrong

  • gjwehnes

    Are you using a single web server (no cluster)

  • Stefoon

    And what are your plans for the variable. Because IIS is an inherantly multi-threaded environment and you just introduced a shared variable with no synchronization on the access or updating process. Depending on exactly what you're going to do, this could introduce some challenging bugs to your application. Using the Application variable addresses some of these, because access and updating uses synchronization. But it would still be useful to know where you're trying to go so that we can suggest the best way to get there.

  • runar lyngset

    You could store it in the Application object.

    Application.Add("WaitingTimeStamp",WaitingTimeStamp);

    and

    DateTime dtTimeStamp=(DateTime
    )Application["WaitingTimeStamp"];


     


  • Global variables in Web Service class