By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,306 Members | 1,621 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,306 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #2

P: n/a
Follow this link to understand the behavior
http://home.networkip.net/dotnet/tidbits/session.htm

"Jeff Smythe" <Sm*******@YaRight.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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

Nov 18 '05 #3

P: n/a
Thanks Alvin,

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

Happy New Year!

Jeff

"Alvin Bruney" <vapor at steaming post office> wrote in message
news:uA**************@tk2msftngp13.phx.gbl...
Follow this link to understand the behavior
http://home.networkip.net/dotnet/tidbits/session.htm

"Jeff Smythe" <Sm*******@YaRight.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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


Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.