PRB: Storing STA COM Component in Session Locks Session Down to Single Thread

This article was previously published under Q243815
This article has been archived. It is offered "as is" and will no longer be updated.
SYMPTOMS
When storing a Single Threaded Apartment (STA) Component Object Model (COM) component in Session scope, the entire Active Server Pages (ASP) application is locked down to a single thread for a given user's session.
CAUSE
When an STA is stored in Session scope, ASP can access the object only by the same thread that created the object causing thread-affinity. Because ASP threads are STA threads, the apartment model object lives on the ASP STA thread. If the object is to be used again in the future by the same user (session), then the user has to be routed to the same thread.
STATUS
This behavior is by design.
MORE INFORMATION
This behavior is one of the reasons that storing STA objects in Session is not recommended. For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
243543 INFO: Do Not Store Single-Threaded Apartment Objects in Session

Steps to Reproduce Behavior

  1. Open Visual Basic, and create a new ActiveX DLL project.
  2. Call the project ThreadProject and the class ThreadClass.
  3. Copy and paste the following into the class module:
    Private Declare Function GetCurrentThreadId Lib "kernel32" () As LongFunction ThreadIdentity() As Variant    ThreadIdentity = GetCurrentThreadIdEnd Function
  4. Make the ThreadProject DDL.
  5. Copy and paste the following VBScript sample into an ASP page:
    <%  Set Obj = Server.Createobject("ThreadProject.ThreadClass")  Response.Write "ThreadID: " & Obj.ThreadIdentity  Set Obj = Nothing%>						
  6. Request the ASP from a browser and click refresh multiple times to see that each request receives a different ThreadID.
  7. Copy and paste the following VBScript sample into another ASP page:
    <%  Set Session("Obj") = Server.Createobject("ThreadProject.ThreadClass")%>						
  8. Request this ASP page from a browser to create the Session scoped object reference. You have now locked down this Session to a single thread.
  9. Request our first ASP page (step 5) and click refresh multiple times, you'll now see that the ThreadID remains unchanged.
REFERENCES
For additional informations, click the article number below to view the article in the Microsoft Knowledge Base:
243543 INFO: Do Not Store Single-Threaded Apartment Objects in Session
243548 INFO: Design Guidelines for VB Components Under ASP
Properties

Article ID: 243815 - Last Review: 01/10/2015 00:49:03 - Revision: 4.1

  • Microsoft Active Server Pages 4.0
  • Microsoft Internet Information Server 3.0
  • Microsoft Internet Information Server 4.0
  • kbnosurvey kbarchive kbaspobj kbprb KB243815
Feedback