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?
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.
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?
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).
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.
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.
Member
26 Points
81 Posts
Static Session Class
Aug 05, 2020 10:35 AM|JamieP1|LINK
Hi
I would like to transfer some data between a few pages. I can use a Session for this reason, with the below code
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
All-Star
48570 Points
18081 Posts
Re: Static Session Class
Aug 05, 2020 01:13 PM|PatriceSc|LINK
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.
Member
26 Points
81 Posts
Re: Static Session Class
Aug 05, 2020 02:04 PM|JamieP1|LINK
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
All-Star
48570 Points
18081 Posts
Re: Static Session Class
Aug 05, 2020 02:44 PM|PatriceSc|LINK
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).
Member
26 Points
81 Posts
Re: Static Session Class
Aug 06, 2020 10:05 AM|JamieP1|LINK
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
All-Star
48570 Points
18081 Posts
Re: Static Session Class
Aug 06, 2020 11:44 AM|PatriceSc|LINK
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.