CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Peter's Gekko

public Blog MyNotepad : Imho { }

Weird session behaviour in VS 2005

In a VS 2005 I had problems getting to the session object. At first it looked like a security issue with IE, but that did not fix the problem. A little experimenting revealed someting weird.

The situation : fresh Virtual PC, all updates applied, no IIS.

Take this code

Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        ListBox1.Items.Add(Session.SessionID)
End Sub

It adds the seesion ID of the roundtrip to the listbox. Every roundtrip has a new ID. I need that ID to identify the session.

Trying to see more I stored something in the session.

Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    ListBox1.Items.Add(Session.SessionID)

    Const MySessionMember As String = "JustME"
    Try
        Label1.Text = Session.Item(MySessionMember).ToString
    Catch ex As Exception
        Label1.Text = "Not found.."
    End Try
    Session.Item(MySessionMember) = TextBox1.Text

End Sub

This works perfectly and the weird thing is that the SessionID is now constant over the roundtrips. And becomes usable.

I guess this a bug /over-optimization in VWD ?

Peter



Comments

Peter van Ooijen said:

But I _am_ using the session object. The only thing I need for the moment is its ID. Which should work...
# July 12, 2004 8:00 AM

Mark Bonafe said:

I would argue that unless you actually put something into the object, you *are not* using the object.

Think of it this way. You decide to build a simple object. You want to create a hashtable and assign an ID to it. You add a few methods to save and retrieve the internal hashtable. Well, when you go to save it, you examine the hashtable. Guess what, there's nothing in it! So you don't create a file. In your retrieve method, you don't have a file to retrieve, so you simply create a new ID.

Work your saving of the ID the same way. If there isn't anything in session, there's nothing to save.
# July 12, 2004 8:55 AM

Peter van Ooijen said:

OK, I can agree on that.
Let's state my problem this way : I want to idenfify the session. So I use the ID of the session object. That does sound imho logical and in line with object model asp.NET.

Is there another way of identifying the session ? I guess there is. Would that be a logical way to identify the session ? I don't think so.

# July 12, 2004 9:10 AM

Mark Bonafe said:

Well, I'm not sure why you want or need to identifiy the session. And I'm not sure I want to know all the details. I've written plenty of code that requires oddities like this. But it does make me wonder - Why id the session if there's nothing in it? Isn't there anything you load into the session (user id, name, etc.)?
# July 12, 2004 10:35 AM

Peter van Ooijen said:

I need a server side filename per session. It is a piece of _demo_ code. Before asp.net storing state in a file was a away to manage state.

I had hoped a reliable way to get me such a filename was using session.session-Id. Unique and fit as a filename.

No production code :>
# July 12, 2004 2:05 PM

Mark Bonafe said:

Ok. That's all well and good. I think I understand now. How about this. When the page is loaded:

int ThisSession = Session.SessionId
Session.Item("ThisSession") = ThisSession
ServerSideFileName = Session.SessionId.ToString

You're only wasting 4 bytes of memory and you lock the SessionId. You also get the reliable filename you wanted.
# July 12, 2004 4:45 PM

Mark Bonafe said:

ooops! Sorry, the SessionID isn't an int. But you get the picture...
# July 12, 2004 4:47 PM

Peter van Ooijen said:

Get the picture. Might just as well store 12 in it, or 34. It will be more than 4 bytes "wasted", besides the value the name also ends up in the session object.

But still... I consider it a bug... :/
# July 13, 2004 2:47 AM

Conal said:

For some reason the Button_Click event does not set the Session variable if you are planning to click a button and store some information. I have no idea why it doesn't - if you put it in a Button_Unload event, it works from page to page (if you just have one button)
# July 28, 2006 9:23 AM

pvanooijen said:

@Conal : ?? Not enough info to see what you're getting at.
# July 29, 2006 6:44 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

Our Sponsors

Free Tech Publications

This Blog

Syndication

News