Interpret session data from ASPStateTempSessions

S

Seetha J

I work on a website where users can apply for different types of loans. Depending on the type of loan we store various sets of data in Session state which is maintained in SQL Server. If the user stops in the middle of loan application process I want to capture whatever data was in the session and store it in our backoffice systems for non-repudiation purposes.

In the past we have been doing it on session end but after we moved to SQL Server Session state, session_onend doesnt get fired. I tried using the expiry of Application Cache as suggested in http://www.eggheadcafe.com/articles/20030416.asp but couldnt get hold of Session object in the Callback function.

Is there a way to get hold of Session data directly from ASPStateTempSessions table? When I tried to deserialize whatever data that comes out of SessionItemShort column I keep getting the following error.

{"Binary stream does not contain a valid BinaryHeader, 101 possible causes, invalid stream or object version change between serialization and deserialization. " }

Here is the code snippet

-----------------------
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select SessionID, Created, Expires, SessionItemShort From ASPStateTempSessions";
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
sessionId = dr.GetString(0);
byte[] sessionData =(byte[]) dr[3];

int ArraySize = sessionData.GetUpperBound(0);

FileStream fs = new FileStream(@"C:\Temp\TestingSession.log", FileMode.OpenOrCreate, FileAccess.ReadWrite);
fs.Write(sessionData, 0,ArraySize);
fs.Close();

BinaryFormatter bf = new BinaryFormatter();
Object obj = bf.Deserialize(fs); //FAILS HERE

...

}
------------------


Any help you can provide is greatly appreciated.

Thanks
Seetha.
 
R

Robbe Morris [C# MVP]

As the author of that article, the only reason I can think as to why session wasn't available
is that your current session timeout is less than the cache expiration (last touched).

If you set the session timeout to a greater number, you can optionally call Session.Abandon
after your custom code in the cache callback. Thus, you wouldn't really be holding onto
session longer than you currently are.

--
2004 and 2005 Microsoft MVP C#
Robbe Morris
http://www.robbemorris.com
http://www.masterado.net



I work on a website where users can apply for different types of loans. Depending on the type of loan we store various sets of data in Session state which is maintained in SQL Server. If the user stops in the middle of loan application process I want to capture whatever data was in the session and store it in our backoffice systems for non-repudiation purposes.

In the past we have been doing it on session end but after we moved to SQL Server Session state, session_onend doesnt get fired. I tried using the expiry of Application Cache as suggested in http://www.eggheadcafe.com/articles/20030416.asp but couldnt get hold of Session object in the Callback function.

Is there a way to get hold of Session data directly from ASPStateTempSessions table? When I tried to deserialize whatever data that comes out of SessionItemShort column I keep getting the following error.

{"Binary stream does not contain a valid BinaryHeader, 101 possible causes, invalid stream or object version change between serialization and deserialization. " }

Here is the code snippet

-----------------------
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select SessionID, Created, Expires, SessionItemShort From ASPStateTempSessions";
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
sessionId = dr.GetString(0);
byte[] sessionData =(byte[]) dr[3];

int ArraySize = sessionData.GetUpperBound(0);

FileStream fs = new FileStream(@"C:\Temp\TestingSession.log", FileMode.OpenOrCreate, FileAccess.ReadWrite);
fs.Write(sessionData, 0,ArraySize);
fs.Close();

BinaryFormatter bf = new BinaryFormatter();
Object obj = bf.Deserialize(fs); //FAILS HERE

...

}
------------------


Any help you can provide is greatly appreciated.

Thanks
Seetha.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top