Classic ASP Store objects in the session object

Danny picture Danny · Jul 28, 2009 · Viewed 13.7k times · Source

I am new to classic ASP and I need to code a web application in classic asp because the customer wants it to be in classic asp. :(

Anyways! here is my question:

When I have a object of a class called person:

Class Person
 Private m_sFirstName

 Public Property Get firstName
 firstName = m_sFirstName
 End Property

 Public Property Let firstName(value)
   m_sFirstName = value
 End Property

End Class


set aPerson = new Person
Person.firstName = "Danny"

set Session("somePerson") = aPerson

So far so good...

On the next request , I try to read the session var like :

If IsObject(Session("aPerson")) = true Then
    set mySessionPerson = Session("aPerson")

      Response.Write(TypeName(myTest)) // will output "Person" 
      Response.Write(mySessionPerson.firstName) // will output "Object doesn't support this property or method: 'mySessionPerson.firstName'
End If

Any ideas about what is going would be of great help.

Answer

AnthonyWJones picture AnthonyWJones · Jul 29, 2009

I can't explain why your code doesn't work looks fine to me.

The object is created in a script context which is subsequently torn down after the request is complete. Hence whilst the type name is available the object's function is broken.

I can tell you its not a good idea to store objects in the Session even those not created in script.

Most objects used in ASP exist in a single thread. Once created only the thread that created the object may access the object. To cope with this once you have stored an object in the session ASP affiliates the session with the specific worker thread that created it the object.

When a subsequent request for that session arrives it must now be handled by its specific worker thread. If that thread happens to be busy working for some other request the sessions request is queued, even if there a plenty of other available worker threads.

The overall effect is to damaging the applications scalability where the work load can become unevenly distributed across the worker threads.