Peter's Gekko

Sponsors

The Lounge

Wicked Cool Jobs

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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


Posted Mon, Jul 12 2004 9:10 AM by pvanooijen

[Advertisement]

Comments

Mark Bonafe wrote re: Weird session behaviour in VS 2005
on Mon, Jul 12 2004 7:47 AM
So the server doesn't track the Session until you decide to actually use it. Is this a bad thing? Sounds quite reasonable to me. Why hang on to an object that isn't being used?
Peter van Ooijen wrote re: Weird session behaviour in VS 2005
on Mon, Jul 12 2004 8:00 AM
But I _am_ using the session object. The only thing I need for the moment is its ID. Which should work...
Mark Bonafe wrote re: Weird session behaviour in VS 2005
on Mon, Jul 12 2004 8:55 AM
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.
Peter van Ooijen wrote re: Weird session behaviour in VS 2005
on Mon, Jul 12 2004 9:10 AM
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.

Mark Bonafe wrote re: Weird session behaviour in VS 2005
on Mon, Jul 12 2004 10:35 AM
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.)?
Peter van Ooijen wrote re: Weird session behaviour in VS 2005
on Mon, Jul 12 2004 2:05 PM
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 :>
Mark Bonafe wrote re: Weird session behaviour in VS 2005
on Mon, Jul 12 2004 4:45 PM
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.
Mark Bonafe wrote re: Weird session behaviour in VS 2005
on Mon, Jul 12 2004 4:47 PM
ooops! Sorry, the SessionID isn't an int. But you get the picture...
Peter van Ooijen wrote re: Weird session behaviour in VS 2005
on Tue, Jul 13 2004 2:47 AM
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... :/
Conal wrote re: Weird session behaviour in VS 2005
on Fri, Jul 28 2006 9:23 AM
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)
pvanooijen wrote re: Weird session behaviour in VS 2005
on Sat, Jul 29 2006 6:44 AM
@Conal : ?? Not enough info to see what you're getting at.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Devlicio.us