ASP.NET 2.0 Performance Inspection Questions - Unmanaged Code

From Guidance Share

Jump to: navigation, search

- J.D. Meier, Srinath Vasireddy, Ashish Babbar, Rico Mariani, and Alex Mackman


Have you enabled AspCompat for calling STA COM components?

Make sure that any page that calls an STA COM component sets the AspCompat page level attribute.

  <@%Page AspCompat = "true" %> 

This instructs ASP.NET to execute the current page request using a thread from the STA thread pool. By default, ASP.NET uses the MTA thread pool to process a request to a page. If you are using STA components, the component is bound to the thread where it was created. This causes a costly thread switch from the thread pool thread to the thread on which the STA object is created.

Do you create STA COM components in the page constructor?

Check your pages to ensure you are not creating STA COM components in the page constructor. Create STA components in the Page_Load, Page_Init or other events instead.

The page constructor always executes on an MTA thread. When an STA COM component is created from an MTA thread, the STA COM component is created on the host STA thread. 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, and only one call executes at a time. This effectively bottlenecks the page to a single thread and causes substantial performance degradation.

If you are using the AspCompat attribute, these events run using a thread from the STA thread pool, which results in a smaller performance hit due to the thread switch.

Do you use Server.Create object?

Avoid using Server.CreateObject and early bind to your components at compile time wherever possible. Server.CreateObject uses late binding and is primarily provided for backwards compatibility.

Search your code base to see if you use this routine and as an alternative, create an interop assembly to take advantage of early binding.

Related Items

For more information about the questions and issues raised in this section, see ASP.NET 2.0 Performance Guidelines - COM Interop.

Personal tools