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



The information in this article applies to:

  • Microsoft Active Server Pages
  • Microsoft Internet Information Server 3.0
  • Microsoft Internet Information Server 4.0

This article was previously published under Q243815

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 Long
    Function ThreadIdentity() As Variant
        ThreadIdentity = GetCurrentThreadId
    End 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

243544 INFO: Component Threading Model Summary Under ASP

243548 INFO: Design Guidelines for VB Components Under ASP


Modification Type:MajorLast Reviewed:11/17/2003
Keywords:kbASPObj kbprb KB243815 kbAudDeveloper