Session State - What does it take to establish one single ASP.NET session per "browser session"

J

Jeff Smythe

I simply want to execute some code once when a new session of my ASP.NET
application is started (I'm not using session state for anything else - just
writing some data to a database). I thought that I could simply put the code
in the Session_Start event procedure in Global.asax.cs, however, the event
procedure executes and a new session is created every time any page is
requested - not just for the first page requested.
Response.Write(Session.SessionID); shows a new session ID is generated for
every page requested.



After reading several resources, I have been lead to believe that if I write
to the session dictionary (as in the code in the next line), then the
session state will be established and Session_Start will not fire again
until after the browser has been closed.



Session["MyData"] = "I love ASP.NET Sessions";



After executing that line, Session_Start fires for each subsequent page
requested and I still get a new SessionID for each subsequent page request.
I have separately tried the following variation in Session_Start in
Global.asax.cs:



if (Session["MyData"] == null) {

Session.Add("MyData", " I love ASP.NET Sessions");

// do some other stuff here

}



But, on each subsequent page requested, this if test evaluates to true -
indicating that the session dictionary was never written to. Also,
Session_End never fires - thereby confirming my suspicion that the session
dictionary was never written to.



I'm confused. What will it take to cause Session_Start to fire only once for
any page requested for a given browser session of my application? If it's
going to fire for every page request, then I can live with that as long as I
can create some sort of flag variable that gets stored in the session state
that indicates whether the database update code has already run for the
current session. However, when I attempt to write to the dictionary (per the
code above), the test acts as if the dictionary has never been written to
during previous requests - thereby rendering the test useless.



FWIW, I have verified the following:

1. WebConfig has sessionState mode="InProc"
2. None of the pages in the application have the following directive:
[@Page...EnableSessionState="ReadOnly"...]

3. IIS application configuration options has the default [Enable Session
State] checked, with the default timeout of 20 minutes for the application
in question.



Running VS.NET 2003 on XP Pro/SP1.



Thanks in advance!



Jeff
 
N

Natty Gur

Hi,
Session is set by ASP.NET for browser (user) request and maintain by
ASP.NET for the same browser by cookies that send to the client. So
session represent certain explorer opened by user. If you open new
window for each request you will end up with new session for each
browser.
Also, if for some reason your client doesn’t support cookies every
request to server will end with new session. ASP.NET support cookieless
situation by embedding data to URL address. You can adjust web.config to
set session support to be cookieless.
By default sessions ends 20 min. after the last request processed on the
server for the session user. You can change this by adjusting
web.config.

Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)58-888377
 
J

Jeff Smythe

Thanks Alvin,

I appreciate your response which actually addresses the specific question I
posted.

Happy New Year!

Jeff

Alvin Bruney said:
Follow this link to understand the behavior
http://home.networkip.net/dotnet/tidbits/session.htm

Jeff Smythe said:
I simply want to execute some code once when a new session of my ASP.NET
application is started (I'm not using session state for anything else - just
writing some data to a database). I thought that I could simply put the code
in the Session_Start event procedure in Global.asax.cs, however, the event
procedure executes and a new session is created every time any page is
requested - not just for the first page requested.
Response.Write(Session.SessionID); shows a new session ID is generated for
every page requested.



After reading several resources, I have been lead to believe that if I write
to the session dictionary (as in the code in the next line), then the
session state will be established and Session_Start will not fire again
until after the browser has been closed.



Session["MyData"] = "I love ASP.NET Sessions";



After executing that line, Session_Start fires for each subsequent page
requested and I still get a new SessionID for each subsequent page request.
I have separately tried the following variation in Session_Start in
Global.asax.cs:



if (Session["MyData"] == null) {

Session.Add("MyData", " I love ASP.NET Sessions");

// do some other stuff here

}



But, on each subsequent page requested, this if test evaluates to true -
indicating that the session dictionary was never written to. Also,
Session_End never fires - thereby confirming my suspicion that the session
dictionary was never written to.



I'm confused. What will it take to cause Session_Start to fire only once for
any page requested for a given browser session of my application? If it's
going to fire for every page request, then I can live with that as long
as
I
can create some sort of flag variable that gets stored in the session state
that indicates whether the database update code has already run for the
current session. However, when I attempt to write to the dictionary (per the
code above), the test acts as if the dictionary has never been written to
during previous requests - thereby rendering the test useless.



FWIW, I have verified the following:

1. WebConfig has sessionState mode="InProc"
2. None of the pages in the application have the following directive:
[@Page...EnableSessionState="ReadOnly"...]

3. IIS application configuration options has the default [Enable Session
State] checked, with the default timeout of 20 minutes for the application
in question.



Running VS.NET 2003 on XP Pro/SP1.



Thanks in advance!



Jeff
 

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,020
Latest member
GenesisGai

Latest Threads

Top