Microsoft Knowledge Base Email Alertz

KBAlertz.com: (308095) - When you call apartment-threaded components from an ASP.NET page in ASPCOMPAT mode, you may notice severe performance degradation.

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 308095 - Last Review: February 23, 2007 - Revision: 2.8

PRB: Creating STA Components in the Constructor in ASP.NET ASPCOMPAT Mode Negatively Affects Performance

This article was previously published under Q308095

SYMPTOMS

When you call apartment-threaded components from an ASP.NET page in ASPCOMPAT mode, you may notice severe performance degradation.

CAUSE

If you use ASPCOMPAT mode (that is, if you use a page with the <%@ ASPCOMPAT="true" %> directive), ASP.NET runs those pages on an STA thread pool. However, Component Object Model (COM) components that are created at construction time are created before the request is scheduled to the single-threaded apartment (STA) thread pool and are therefore created from a multithreaded apartment (MTA) thread. In this scenario, you experience substantial performance degradation.

Most significantly, the same thread (host STA) executes all instances of apartment-threaded components that are created from MTA threads. This means that even though all users have a reference to their own instance of the COM component, all of the calls into these components are serialized to this one thread (only one call executes at a time).

Additionally, there is a smaller performance hit each time a call is made to the component from the Page events because of a thread switch. This is because the Page events are executed on a thread from the STA pool, but the COM component is still executed on the host STA (because the COM component was created from an MTA client). This thread switch also leads to other errors if you use impersonation. For more information, see the "References" section of this article.

RESOLUTION

If you are using ASPCOMPAT mode with STA components, only create COM components from a method or one of the Page events (for example, Page_Load, Page_Init, and so on), and do not create these COM components at construction time.

For example, avoid a member declaration similar to the following, which creates the component at construction time:

Visual Basic .NET
<%@ Page Language="VB" ASPCOMPAT="TRUE" %>
<script runat="server">

Dim comObj As MyComObject = New MyComObject()

Public Sub Page_Load()
   comObj.DoSomething()
End Sub
</script>
				
Visual C# .NET
<%@ Page Language="C#" ASPCOMPAT="TRUE" %>
<script runat="server">

MyComObject comObj = new MyComObject();

public void Page_Load()
{
   comObj.DoSomething()
}
</script>
				
Visual J# .NET
<%@ Page Language="VJ#" ASPCOMPAT="TRUE" %>
<script runat="server">

MyComObject comObj = new MyComObject();

public void Page_Load()
{
  comObj.DoSomething();
}
</script>
				
Use the following code instead:

Visual Basic .NET
<%@ Page Language="VB" ASPCOMPAT="TRUE" %>
<script runat="server">

Dim comObj As MyComObject 

Public Sub Page_Load()
   comObj = New MyComObject()
   comObj.DoSomething()
End Sub
				
Visual C# .NET
<%@ Page Language="C#" ASPCOMPAT="TRUE" %>
<script runat="server">

MyComObject comObj;

public void Page_Load()
{
   comObj = new MyComObject();
   comObj.DoSomething();
}
				
Visual J# .NET
<%@ Page Language="VJ#" ASPCOMPAT="TRUE" %>
<script runat="server">

MyComObject comObj;

public void Page_Load()
{
  comObj = new MyComObject();
  comObj.DoSomething();
}
</script>
				

STATUS

This behavior is by design.

REFERENCES

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
325791  (http://kbalertz.com/Feedback.aspx?kbNumber=325791/EN-US/ ) PRB: Access Denied Error Message Occurs When Impersonating in ASP.NET and Calling STA COM Components

APPLIES TO
  • Microsoft ASP.NET 1.1
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft ASP.NET 1.0
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual J# .NET 2003 Standard Edition
  • Microsoft .NET Framework 1.1
Keywords: 
kbhttpruntime kbinterop kbperformance kbprb kbreadme kbthread KB308095
       

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

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please