Microsoft KB Archive/243815

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

Article ID: 243815

Article Last Modified on 11/15/2006

-

APPLIES TO


 * Microsoft Active Server Pages 4.0
 * 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
 Open Visual Basic, and create a new ActiveX DLL project. Call the project ThreadProject and the class ThreadClass.  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  Make the ThreadProject DDL.  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 %>                        Request the ASP from a browser and click refresh multiple times to see that each request receives a different ThreadID.</li>  Copy and paste the following VBScript sample into another ASP page: <% Set Session("Obj") = Server.Createobject("ThreadProject.ThreadClass") %>                       </li> 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.</li> Request our first ASP page (step 5) and click refresh multiple times, you'll now see that the ThreadID remains unchanged.</li></ol>

<div class="references_section">