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
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