0

in asp.net we can store application scope variables (static, application). But how can we sync the static variables of a node with the static variables of another node, without the use of distributed cache and db?

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
sparrows81
  • 431
  • 3
  • 6
  • 19

1 Answers1

0

You can not sync static variables without having the values on a common database or common file storage.

So what you try to make, can not be done. You need to use a common database, or a common file, or a distributed cache that also use common database - something to keep your values on one place, a common file or a common database.

Some note. The static variables are different not only on web farm, but also on web garden. Meaning that each application thread even on the same pool have different static values.

Some related answers : Static variable Behaviour in Asp.Net
Lifetime of ASP.NET Static Variable
Using static variables instead of Application state in ASP.NET

Community
  • 1
  • 1
Aristos
  • 66,005
  • 16
  • 114
  • 150
  • thanks for your answer, but as i wrote i already know it can be achived through distributed cache or db. But i want to know if it is possible to develop an application alternative. Eg calling a web service to each server to set the new value for the static variable. If you cant use cache or db which is the best practice? – sparrows81 Apr 22 '13 at 14:06
  • @user2236886 Even if you call a webservice, you again need to access the values using a common database. Now, in this case there is not best practice, there is only one way - a common file, either an xml, either a file database, either a full database, but a common file. – Aristos Apr 22 '13 at 14:12
  • i've done this but i don't know if it is bad: an administrator on node 1 update a variable eg. paypal email. asp.net write this configuration to db, to ensure data consistency over time. Then asp.net call a web service, one for each node in the web farm. This web service update the value of the static variable with the new value. When a user go to node2 has the value updated and don't need to acces the db. Is this cool? Is this ok or have some drawback? – sparrows81 Apr 22 '13 at 14:21
  • @user2236886 Its sound good. The issues on this stuff is to make a procedure that understand the WHEN the value must be read again. The when and if the data is up to date the moment you read them is the issue that you need to solve. – Aristos Apr 22 '13 at 14:24