Answered by:
Static Session Class

Question
-
User-1526035670 posted
Hi
I would like to transfer some data between a few pages. I can use a Session for this reason, with the below code
public class ExtStorage { public propertyOne {get; set;} public propertyTwo {get; set;} public static TempStorage CreateTempTrail { get { var session = (TempTrail)HttpContext.Current.Session["__TempStorage__"]; if (session != null) return session; session = new TempTrail(); HttpContext.Current.Session["__TempStorage__"] = session; return session; } } }
In the past i have had odd errors or scenarios so wonder if this is the correct way of using a Session class with a static method especially when there would be multiple users to this page possibly at the same time? Or if there is a better way?
Thanks
Wednesday, August 5, 2020 10:35 AM
Answers
-
User753101303 posted
Hi,
Seems fine (though the code won't compile because of the missing property types). Maybe you used static data which would be shared accross all users.
Or what happens if the session expires and properties are set back to their default value?My personal preference is to always use values that could be reloaded on demand if the session expires.
Edit: it's likely best to deal with errors as they happen (with the error message or a description of the bad behavior) rather than after or before the fact.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, August 5, 2020 1:13 PM -
User753101303 posted
Yes each user have its own session.
The problem is not directly with "static". The common catch is to use static "data" ie public static string MySample {get;set;}
Here all users would share the same string. Then if you change the implementation to store/retrieve the value from the session you won't have this problem any more even though it is still a static property.
So in short the static keyword itself is not a problem. The problem is when you have somewhere a static "data storage" (unless of course your intent is really to share the same value accross all users).
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, August 5, 2020 2:44 PM -
User753101303 posted
In short what matters is the underlying storage. It is a static property but its implementation returns an object taken from a storage that depends on the browser session. So even though it is stactic it won't return the same object depending on which browser (and so which user) is doing this request.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, August 6, 2020 11:44 AM
All replies
-
User753101303 posted
Hi,
Seems fine (though the code won't compile because of the missing property types). Maybe you used static data which would be shared accross all users.
Or what happens if the session expires and properties are set back to their default value?My personal preference is to always use values that could be reloaded on demand if the session expires.
Edit: it's likely best to deal with errors as they happen (with the error message or a description of the bad behavior) rather than after or before the fact.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, August 5, 2020 1:13 PM -
User-1526035670 posted
Hi
Yes i agree with you. That was a typo regarding the missing types (oops).
If a session is stored on the server (I believe this is InProc) then i assume if 2 users came onto the same page at the same time whatever the values of the session variables are would be different for each user rather than using the same value for both? This is what i would like to avoid so i wasnt sure if the static keyword was causing some conundrum somehow?
Many thanks
Wednesday, August 5, 2020 2:04 PM -
User753101303 posted
Yes each user have its own session.
The problem is not directly with "static". The common catch is to use static "data" ie public static string MySample {get;set;}
Here all users would share the same string. Then if you change the implementation to store/retrieve the value from the session you won't have this problem any more even though it is still a static property.
So in short the static keyword itself is not a problem. The problem is when you have somewhere a static "data storage" (unless of course your intent is really to share the same value accross all users).
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, August 5, 2020 2:44 PM -
User-1526035670 posted
Hi Patrice
To ensure i've understood you correctly, the way i have my current static method is fine and will ensure each user would have their own session and you're not seeing any "data storage" variables that may leak data from one session to another? All properties would be as they are and non static.
If i add another method to clear the session again i would make it static and call this once the process is complete.
Once i know this, will mark this as an answer.
Many thanks
Thursday, August 6, 2020 10:05 AM -
User753101303 posted
In short what matters is the underlying storage. It is a static property but its implementation returns an object taken from a storage that depends on the browser session. So even though it is stactic it won't return the same object depending on which browser (and so which user) is doing this request.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, August 6, 2020 11:44 AM