ASP/VBScript Session collection storing objects

Status
Not open for further replies.

Pont

Ars Legatus Legionis
26,160
Subscriptor
I'm storing a Class I defined myself in the Session collection. However, when I retrieve it from the Session collection, it doesn't have any fields or properties or methods.

code:<HR>
Code:
dim foo
Set foo = new PurchaseRequisition
' assign some fields to foo
Set Session("NEWREQ") = foo<P>dim bar
Set bar = Session("NEWREQ")
Response.Write isObject(bar) ' prints "true"
Response.Write typeName(bar) ' prints "PurchaseRequisition"
Response.Write bar.somefield ' "Object doesn't support this property or method"

So it's still an object of type PurchaseRequisition, but it doesn't have any of the fields/properties/methods. WTF?
 

kleel

Ars Scholae Palatinae
1,022
I always thought that sticking COM objects into a Session is a Very Bad Thing(tm) they tend to bring the server down as the resourses needed to maintain a seperate object for each seperate session are prohibative.

what is it that you are trying to achive?

is the usage of cookies/querystring not sufficent for your needs?

also, aren't COM objects in ASPs are supposed to be instaniated using:
set foo=server.createobject("Myserver.MyObject")

just my 0.02p ..
 
I haven't used VBscript objectsin sessions,but I have used JScript objects in the past - using sessions on iis is generally not the rightthing to do, but sometimes it is (when you know he app isgoing to live on one server only, and you can't be bothered implementing your own session system).

Anyway, the problem may well lie in this bit of code:

' assign some fields to foo

Where are you getting the fields from - if you are assigning them from an object with page scoep, like a recordset, or form variable, this is probably the cause. In that case, you are assigning a reference to that object, not a copy of its contents.

e.g.


code:<HR>
Code:
<P>foo.bar = Request.Form("bar") ' wrong, this object will be out of scope in the next page.<P>foo.bar = CStr(Request.Form("bar")) ' that should work, the copied value should remain in the next page.<P>

Sorry, my vb is very rusty, I can never remember where to use set or if you should use parenthesis or not. I really, really, don't like VB, and its nasty unpredictable syntax is the main reason. I'm sure you get the idea though.
 

Pont

Ars Legatus Legionis
26,160
Subscriptor
They're not COM objects. They are VBScript "objects" (created in VBScript using a Class statement).

I was storing them in cookies, but if the user adds to many line items to their purchase requisition it will reach the size limitation for cookies.

These objects do not lock any threads.

This will always run on only 1 webserver.

No more than 20 users will ever be using this app per day. Only 1 object per user is stored in the Session.

Currently, I'm using my home-grown toText() and fromText() methods to store them in the Session as text. (My kingdom for "implements Serializable")

I had hoped to avoid storing them in the database, as that would leave temporary cruft in the database if someone stopped in the middle of creating a new purchase requisition. I would either have to have some sort of daemon/service/scheduled batch job to clean out the cruft or have the script itself clean out the cruft each time it's run. We're trying to keep it all ASP/VBScript if possible, with no external code.

The Session is a sub-optimal place to store it really, since I don't want to adjust the timeout and I do want users to be able to wait more than 20 minutes before continuing their new purchase requisition. I guess I'll have to store it in the database anyhow.
 

temp

Seniorius Lurkius
44
They're not COM objects. They are VBScript "objects" (created in VBScript using a Class statement).

That's your problem right there. You can't store vbscript objects in the session object -- only COM objects and normal variables.

Are you trying to store somthing complicated? If not your best option is to encode the information as a string and store it like that. I can send you some code to show you how you can work with this.

If you feel a bit more ambitious there are actually options for using COM objects in the session variable without getting the nasty problems mentioned above. It involves knowing about nasty issues such as COM threading models, but I could probably dig up some relevant links if anyone is interested.
 

Pont

Ars Legatus Legionis
26,160
Subscriptor
Actually, I'm using a home-grown toXML() and fromXML() routines (wrote my own very simple XML parser in VBScript :O ) and storing it as text in the session right now.

The easy thing for me to do, at this point, is to store the objects as text (simple XML) in the database, as that will let me leverage 99.9% of my existing code and will solve the Session timeout issue. It's not The Right Way, but it's the easy way.

Is there anywhere in the MSDN where it says you can't store VBScript objects in the Session variable? It woulda been nice to know that. Seeing as you can store VBScript objects in Dictionary objects and Arrays and such.
 

temp

Seniorius Lurkius
44
I looked around in MSDN and the smoking gun is in Q246596 "Cannot Use VBScript Classes with Application or Session Scope". Moreover it turns out that MS prevents you from storing vbscript classes on purpose.

The problem is that these objects are apartment threaded, and if you store apartment threaded objects in the Session or Application objects you can lock IIS down to a single thread. And you really, really don't want to do that. (You can in fact store COM objects in the Session and Application variables provided they are marked as "Both" threaded, but it's safer to just avoid storing anything but simple strings, numbers and the occasional array.)

As for storing the XML documents in the database instead of in the Session object you are probably doing the right thing anyway. I've seen machines grind to a virtual halt because people used the Application and Session objects for storing all sorts of stuff.

BTW if you are doing XML parsing and generating you should definitely look into Microsoft's XML SDK and the MSXML component suite. It's a pretty fast parser, and the components are easy to use from vbscript. Plus you don't have to write your own XML parser/generator, which can save a lot of effort. The DOM objects in particular could be just what you are looking for.
 
Well, you _can_ do it with jscript classes, if I remember correctly (I haven't had the need to do that for a while, but I'm sure I have done it in the past for some reason).

You can write a method to serialise your class to and from the session though, if you need that, but if you say you want to persist your class beyond the session timeout, you would be better off serialising to a database anyway.

And, for the record, I don't think there is any harm in storing transtory stuff in the Session (I have gone through the whole "everything must be stateless" thing), but you must be aware of the tradeoffs you are using with sessions.

OTOH, as has been repeated ad nauseum, using com objects as opposed to simple native objects (strings, ints etc.) is asking for trouble, unless they are are marked as both apartment and free threaded, and it isn't usually worth the effort of doing this.
 
Status
Not open for further replies.