1

I have read posts like this: Is it OK to use static variables to cache information in ASP.net?.

I am trying to understand the visibility of static variables. There is one ASP.NET process (W3WP) - I realise that web gardens exist but in my scenario there is one - multiple clients can connect. If one of those clients set a static (shared) integer to 10 then will all web clients see the Shared variable as 10? I am trying to understand if a Shared variable is like an Application setting or a Session setting.

Does this also apply to VB.NET (client application). I have read an article, which talks about allowing multiple VB.NET clients to use a single app domain. Are static variables shared across all clients with a single app domain?

Community
  • 1
  • 1
w0051977
  • 15,099
  • 32
  • 152
  • 329

1 Answers1

3

a static / shared variable is per-AppDomain, so yes all clients to that AppDomain will see the same value. It is broadly comparable to an application setting, yes.

(Caveat: unless the field is marked [ThreadStatic], but that is a really bad idea in a web application.)

You should be very cautious of using static / shared fields, especially in a threaded environment. In addition to state bleeding incorrectly between users / sessions, they have inherent thread-safety issues, and you would typically want to synchronize access to them very carefully. For example, a static List<T> would be really risky if multiple threads can add/remove/etc at the same time.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900