Static Vs Public Variables in a Multiuser Environment

I use the following approach to keep global variables in a separate class file so that they can be passed among several base classes. The problem is that it does not work when the system is used in a multi-user environment. I cannot choose the Session approach because administrators are not ready to enable session state on the SharePoint server.

static class Global { private static string id = string.Empty; public static string id { get { return id; } set { id = value; } } } 

If I remove the static ad from the code above, will it work correctly in a multi-user scenario? What other parameters do I have, without requesting any help from the server administrators.

+4
source share
4 answers

Do not use static for things that are intended for each user or for each request; static quite rare in a web application, but can sometimes be used for things like saving a snapshot of the overall system configuration (preferably immutable).

So: where can we store things?

The state of the session would be the obvious answer to what is (and here is the key) per session, but it seems that this is not an option

In addition, you have basically: an http request. This means that you have access to the form, query string and cookies. And so it is. You do not want to store much in cookies: it inflates every request. And the "viewstate" is just evil. If the regular state of the session is not viable, you can execute some implementation of the home brew "session" based on some cookie (suitably provided), but frankly, I think it will be useful for you too:

  • use the request form / query string if all you need is id
  • else activate administrators to activate session state
+7
source

Your static variables will be shared by all users and will not belong to a specific user. Using static variables to maintain state in a web environment is almost always a bad idea.

If you cannot enable session state, a good strategy would be:

  • Add user id to url and save it in url
  • Save the state on the server to the database based on this id

That way, you are essentially reinventing session state; but you avoid enabling session state all over the world. If the state is small enough, you can also encode it in a URL.

Also, consider that session state does not work well in a server farm environment, if you do not store it on a shared server, this may be the reason that administrators are not inclined to activate it.

+5
source

Static is potentially even worse in a web farm or web garden environment that SharePoint supports pretty well. Static fields are for AppDomain. This means that, for example, they will be the same for all users within the same workflow on the same machine, but will differ between workflows and between machines.

Do not use static in a multi-user application, and you don’t have to worry about changes in behavior when the environment changes.

+2
source

If you need session state, enable session state. It is so simple.

However, if all you need is just a few (lite) session variables, you can get rid of cookies and pass data back and forth for each request.

You can also try to “override” the state of the session using something like:

 static class Globals { private static Dictionary<string, MySessionObjectType> sessions; public static MySessionObjectType GetSessionData(string SessionID){...} public static void SetSessionData (string SessionID, MySessionObjectType sessionData){...} } 

This, of course, does not scale for multiple web servers, and session timeout management will be PITA.

Do not forget that the nature of the website itself does not have a status, so using too much state (either on the server or on the client) is not always a wise choice.

+1
source

All Articles