title: Unable to implement static variables in wcf C#
body: <p>Here is my problem.
  I am implementing a simple client server using wcf.The server exposes a 'sum' function in which any value sent by a client is added to a global sum and this sum is returned to all clients connected to the server at the time.
  This is the function </p>
  
  <pre><code>  public void AddMessage(int num)
      {
          sum = sum + num;
          //Go through the list of connections and call their callback funciton
          subscribers.ForEach(delegate(IMessageCallback callback)
          {
              if (((ICommunicationObject)callback).State == CommunicationState.Opened)
              {
                  Console.WriteLine("Calling OnMessageAdded on callback ({0}).", callback.GetHashCode());
  
                  callback.OnMessageAdded(sum, DateTime.Now);
              }
              else
              {
                  subscribers.Remove(callback);
              }
          });
  
      }
  </code></pre>
  
  <p>This is the callback function that server calls</p>
  
  <pre><code> public void OnMessageAdded(int sum, DateTime timestamp)
      {
          Console.WriteLine(sum+ ": " + timestamp.ToString("hh:mm:ss"));
      }
  </code></pre>
  
  <p>There is no problem till this part.
  I now tried running a client and server on a different port(8001)
  The client would initially contact server listening to port 8000 and once it gets the sum,update the server listening on 8001.</p>
  
  <p>Here's the code that does that.</p>
  
  <pre><code>  //This is the function that the SERVER will call
      public void OnMessageAdded(int sum, DateTime timestamp)
      {
          Console.WriteLine(sum + ": " + timestamp.ToString("hh:mm:ss"));
          WCFService.RCRServer.Sum=sum;
          Console.WriteLine("8001's sum " + WCFService.RCRServer.Sum);
      }
  </code></pre>
  
  <p>As you can see 'sum' is a static field in RCRServer.This is how it's declared.</p>
  
  <pre><code>        private static int sum =0;
  
      public static int Sum
      {
          get { return sum; }
          set { sum = value; }
  
      }
  </code></pre>
  
  <p>This updating also is fine.The problem starts when the connection to port 8000 is lost and i try calling the same service on port 8001.I expected that since the client keeps updating the static field for the server on port 8001,when a client calls the function on this server(8001) the old sum would be available.But it gets reset to 0 and starts the counting again.Where am I going wrong here.Any advice would be much appreciated.
  Thanks.</p>