Bob.at.Indigo.Health
Bob.at.Indigo.Health

Reputation: 11895

ASP.NET global variables in a web form

Simple question, but after several hours of searching I haven't found a simple answer. Is there an easy way to share global variables amongst all of the sessions in an ASP.NET MVC 4 website running on multiple servers?

On a single server, I'd just create a static class with appropriate thread synchronization wrapped around the member accessors. But that obviously doesn't synchronize between servers.

You'd think that since it's so easy to save per-session state and have it magically synched between servers that you'd be able to do the same thing with application state. But the stuff I've read all says that application state is server-local.

Upvotes: 1

Views: 979

Answers (3)

shrisha
shrisha

Reputation: 445

Sounds like it's time to move some of the data to a persistent store (Database, filesystem) and optionally add a caching layer (We are happy AppFabric users) on top for performance.

Upvotes: 0

Aristos
Aristos

Reputation: 66641

The answer is that you need a common database where you write and read your common variables.

By mean common database, you can even use a simple xml file - but its must be a file, a common shared file among your servers, this is the most simple way.

Upvotes: 0

chue x
chue x

Reputation: 18803

If you mean storage for session state, you have 3 options:

  • In-Process Storage (which sounds like what you are using)
  • Session State Service
  • Microsoft SQL Server

The latter 2 provide the "shared globals" that you are looking for. Note though that the Session State Service is a single point of failure. The SQL Server session state storage can be redundant, if your SQL configuration is redundant.

See also:

If you want to move to other solutions that aren't natively supported by ASP.Net, you could use:

Note I have not used either Memcached or Redis, but I've heard great things.

Upvotes: 1

Related Questions