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

Discussion in 'ASP .Net' started by Jeff Smythe, Jan 1, 2004.

  1. Jeff Smythe

    Jeff Smythe Guest

    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
    Jeff Smythe, Jan 1, 2004
    #1
    1. Advertising

  2. Jeff Smythe

    Natty Gur Guest

    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!
    Natty Gur, Jan 1, 2004
    #2
    1. Advertising

  3. Jeff Smythe

    Alvin Bruney Guest

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

    "Jeff Smythe" <> wrote in message
    news:%...
    > 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
    >
    >
    Alvin Bruney, Jan 1, 2004
    #3
  4. Jeff Smythe

    Jeff Smythe Guest

    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:...
    > Follow this link to understand the behavior
    > http://home.networkip.net/dotnet/tidbits/session.htm
    >
    > "Jeff Smythe" <> wrote in message
    > news:%...
    > > 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
    > >
    > >

    >
    >
    Jeff Smythe, Jan 2, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S01aX3N0YXRl?=

    Quick one - Is SESSION per browser instance or per IP Address?

    =?Utf-8?B?S01aX3N0YXRl?=, Apr 4, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    5,850
    gerry
    Apr 10, 2006
  2. Replies:
    5
    Views:
    2,508
  3. Replies:
    0
    Views:
    331
  4. chris brat
    Replies:
    1
    Views:
    608
    chris brat
    May 10, 2006
  5. Randy Kramer
    Replies:
    2
    Views:
    383
    Randy Kramer
    Jan 12, 2007
Loading...

Share This Page