Need Microsoft Help: Session object retrieving another user's Session information

B

Boban Dragojlovic

I'm building a complex web-based reservations system.

Gathering the user's data requires between 8 and 15 pages (depending on
which options they are interested in). I use the "Session" object to store
the various elements as the user moves through the pages.

Rather than storing the preferences directly in the Session object (e.g.
Session("LastName") = ...), I created a class

<Serializable()> Public Class ReservationInfo

This class has private variables for all the data, and exposes properties to
retrieve/manipulate the data. I did this for several reasons:
* I don't have to worry about screwing up variable names from one page
to the next (is it Session("OptionNumber") or is it Session("OptionNbr")) --
* I can add some logic related to the assignment of properties (e.g. if
a property changes, I might need to reset other properties)


In one of my modules I declare

dim g_Info as ReservationInfo

And on each page, then, in the OnLoad event, I have something to this
effect:
g_Info = Current.Session("ReservationInfo")

(actually, in the OnLoad event I call a public sub in one of my
modules, and it does the retrieval. That's why it references
Current.Session)


and in the Unload event, I have
Session("ReservationInfo") = g_Info


This way my entire set of variables is loaded from the Session, and saved
back to the session each time.

I let .NET manage the Session object in memory (single server), and
everything works great (or at least it did during my development/testing).



The problem happened when we ran the first multi-user test (e.g. 5 people
testing at the same time).



Suddenly, as users moved from page to page, their session information was
getting swapped with the information from another user. Every so often
(every couple of pages or so), as you moved from one page to another, you
suddenly wound up with the data of another user on the system.


I then added logic so that in the unload event, it would serialize and dump
the entire g_Info object to disk, and the filename would be based on the
SessionID of the user. That way I could trace the session information of
each user as they moved from page to page. I've confirmed that this happens
there. It seems that when it retrieves the data from the Session object
g_Info = Current.Session("ReservationInfo")
it sometimes grabs the wrong data.

I've analyzed several hundred page requests, and found many occurrences, but
can't find any pattern to it.


I don't have a clue how this could be. Any ideas?
 
M

Matt Berther

Hello Boban,
there. It seems that when it retrieves the data from the Session
object
g_Info = Current.Session("ReservationInfo")
it sometimes grabs the wrong data.
I've analyzed several hundred page requests, and found many
occurrences, but can't find any pattern to it.

Could this be because of the global g_Info variable?

For what its worth, I implement a similar pattern when doing session state, however, I expose my object using a singleton pattern.

Code Example:

class SessionManager
{
private const string SESSION_MANAGER = "SESSION_MANAGER";
private string username;

private SessionManager()
{
}

public string UserName
{
get { return username; }
set { username = value; }
}

public static SessionManager Instance
{
SessionManager manager = HttpContext.Current.Session[SESSION_MANAGER] as SessionManager;
if (manager == null)
{
manager = new SessionManager();
HttpContext.Current.Session[SESSION_MANAGER] = manager;
}
return manager;
}
}

When I want to access the session state in the code, I type
SessionManager.Instance.UserName = "someValue"; or
string someValue = SessionManager.Instance.UserName;

The idea is quite similiar to yours, and this should resolve your problems.
 
P

Patrice

A module is a class with shared variables (possibly as private members). As
such all users are sharing the same g_Info.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top