Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 243815 - Last Review: November 15, 2006 - Revision: 4.1
PRB: Storing STA COM Component in Session Locks Session Down to Single Thread
This article was previously published under Q243815
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.
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.
This behavior is by design.
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Â
(http://kbalertz.com/Feedback.aspx?kbNumber=243543/EN-US/
)
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.
- Copy and paste the following VBScript sample into another ASP page:
<%
Set Session("Obj") = Server.Createobject("ThreadProject.ThreadClass")
%>
- 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.
- Request our first ASP page (step 5) and click refresh multiple times, you'll now see that the ThreadID remains unchanged.
For additional informations, click the article number below
to view the article in the Microsoft Knowledge Base:
243543Â
(http://kbalertz.com/Feedback.aspx?kbNumber=243543/EN-US/
)
INFO: Do Not Store Single-Threaded Apartment Objects in Session
243548Â
(http://kbalertz.com/Feedback.aspx?kbNumber=243548/EN-US/
)
INFO: Design Guidelines for VB Components Under ASP
APPLIES TO
- Microsoft Active Server Pages 4.0
- Microsoft Internet Information Server 3.0
- Microsoft Internet Information Server 4.0
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate