3

What happens if one user tries to access an ASP.NET page twice before the first page is returned to the client? Have a look at the code below:

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Session("ID") = 1
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Response.Redirect("Default3.aspx")
    End Sub End Class

Partial Class Default2
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Session("ID") = 2
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Response.Redirect("Default3.aspx")
    End Sub
End Class

Imports System.Threading
Partial Class Default3
    Inherits System.Web.UI.Page

    Dim intTest As Integer = 0

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        For intTest = 0 To 10
            Response.Write(Session("ID") & " " & intTest & "<br>")
            Thread.Sleep(1000)
        Next
    End Sub
End Class

Accessing default3.axpx from the same client (PC) concurrently from default.aspx (by clicking button) and default2.aspx (by clicking button) causes the session variable to be the same on both requests (though I set the variable to 1 on the first request and 2 on the second). Is it possible to replicate this behaviour without threading? I believe I have this bug in an asp.net application that does not use threading.

w0051977
  • 15,099
  • 32
  • 152
  • 329
  • You are accessing the same session variable in both forms. That's why one gets overriden depending on the page you load last. You can add an array to a session. – UNeverNo Oct 19 '12 at 18:22
  • @UNeverNo, can this behavior be simulated in a single threaded asp.net app? i.e. by removing the thread.sleep in default3.aspx? – w0051977 Oct 19 '12 at 18:25
  • I don't know at the moment what you're going for. What do you want to test? Do you want to know what happens if a user submits the same page from two different tabs in the browser? – UNeverNo Oct 19 '12 at 18:27
  • @UNeverNo,I am trying to understand what happens to session variables if a user submits a page twice (with different values for the session variables). It appears that the first request is executed and then the second request is executed. Is this correct? – w0051977 Oct 19 '12 at 18:34
  • A session variable stores values so they are still accessable even after the postback. If a user executes it twice with two different values the last one always wins. – UNeverNo Oct 19 '12 at 18:49
  • I realise that. In my question I ask what happens if a user clicks submit in default2.aspx and then submit in default3.aspx (before the first request has finished running)? Will the requests be executed concurrently or will they be executed one after the other? – w0051977 Oct 19 '12 at 18:51
  • And what does your example bring to page1/page2? – UNeverNo Oct 19 '12 at 19:53
  • +1 for the interesting question. Please check my answer. – Win Oct 20 '12 at 01:38

2 Answers2

0

enter image description here

Your question is not about multi-threading; it is about SessionState.

ASP.NET run time uses lock to avoid overriding same session variables although it can handles multiple requests.

It's why you are do not see miss-matching results.

Please also look at -

ASP.NET Application and Page Life Cycle

ASP.NET Application Life Cycle Overview

Win
  • 61,100
  • 13
  • 102
  • 181
  • Thanks. Will the session state be isolated between requests? – w0051977 Oct 19 '12 at 20:12
  • No, session state is shared between pages. Session variables are retrieved at AcquireRequestState event of page life cycle. After each page acquires session variables (one time per page request), the page uses its owns until next page request. – Win Oct 19 '12 at 20:30
  • Can you answer my final question? What happens if a user accesses page 1 and then accesses page one again (before the first page is loaded). Is there a possibility that session state is shared between requests? – w0051977 Oct 19 '12 at 20:39
0

It's important to understand that session information is stored at the application level, and really has nothing directly to do with a page.

You could compare it to a global variable in a class. If all your methods are reading and writing to this variable, it will always contain the information from the last time it was updated. It doesn't matter how many times, or which methods, updated it.

To help see this in your code, create a new class with one property. Change your session variable to be an object of this class. Modify your session logic to refer to this object instead of a string, and update the property.

Finally, put a break point on the setter of your property.

This will let you see whenever your session variable is updated, and with what value. You can also view your stack trace to see what's setting it.

-note, this is all assuming you aren't introducing a farm into this and are accessing session from different machines--

AaronS
  • 7,649
  • 5
  • 30
  • 56