2

Let's say that I have web service with a bunch of methods, and that webservice has a Public Shared Variable on it, if I get a request from Client A and he changes the value of this variable, then will Client B see the value Changed by Client A?

Let me try to explain myself better, example

Let's say I have this variable:

Public Shared state As Boolean = False(Visual Basic)
public static bool state = false; (C#)

And then Client A goes

 state = true (VB)
 state = true; (C#) 

When Client B Check the value of the variable state, will it be true or false?

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
user3044096
  • 171
  • 1
  • 17
  • No, they are not. Note that the `Shared` variable will be used by every call to the service - same value for all users and all requests (until you change it). – John Saunders Apr 24 '14 at 03:26
  • Some code examples of the right/wrong way to use static/shared values: http://stackoverflow.com/a/11738653/453277 – Tim M. Apr 24 '14 at 03:31

1 Answers1

6

The Static variable will be shared across all proxy calls, as long as it is not a web garden and is an in process implementation. The static variable is scoped to the app domain.

In the above case, Client B will see the value set by Client A.

In case, it is a web farm, then the static variable will be per web server and may show weird behavior across web requests.

as a general note, it is advised to be extremely cautious with writable shared values across web requests, due to locking etc.

Raja Nadar
  • 9,409
  • 2
  • 32
  • 41
  • +1. I'm just too slow:) - don't forget to mention server/domain restart case. – Alexei Levenkov Apr 24 '14 at 03:28
  • And, on locking, the OP had better be aware that he's likely to have two threads accessing the variable at the same time, so there can be race conditions if he doesn't interlock access to the variable. – John Saunders Apr 24 '14 at 03:38