Aspcompat in ASPX Page

4.4k views Asked by At

In my aspx pages, I call a COM component that uses STA. So I use aspcompat=true parameter, but I want to understand the logic. What does it exactly do? As far as I know, in STA, it is assumed to be called only by one thread. So I should provide the thread safety myself.

If that's right, where does aspcompat parameter provide the thread safety? On the whole page, or just the point where I access the STA component?

3

There are 3 answers

0
santosh singh On

This directive causes ASP.NET to provide access to ASP-intrinsic objects and changes the thread pool to MTA.

For more details checkout this

0
Kiquenet On

When using single-threaded apartment (STA) COM components, such as components developed using Visual Basic, from an ASP.NET page, you must include the compatibility attribute AspCompat=true in an

<%@ Page>

tag on the ASP.NET page

The AspCompat attribute forces the page to execute in STA mode

ASP.NET by default uses MTA (multi-threaded apartment) threads

When building ASP.NET applications that interface with old school COM objects like those created with VB6 or Visual FoxPro (MTDLL), it's extremely important that the threads that are serving requests use Single Threaded Apartment Threading. STA is a COM built-in technology that allows essentially single threaded components to operate reliably in a multi-threaded environment. STA's guarantee that COM objects instantiated on a specific thread stay on that specific thread and any access to a COM object from another thread automatically marshals that thread to the STA thread. The end effect is that you can have multiple threads, but a COM object instance lives on a fixed never changing thread.

ASP.NET by default uses MTA (multi-threaded apartment) threads which are truly free spinning threads that pay no heed to COM object marshaling. This is vastly more efficient than STA threading which has a bit of overhead in determining whether it's OK to run code on a given thread or whether some sort of thread/COM marshaling needs to occur. MTA COM components can be very efficient, but STA COM components in a multi-threaded environment always tend to have a fair amount of overhead.

STA in ASP.NET

Support for STA threading in the ASP.NET framework is fairly limited. Specifically only the original ASP.NET WebForms technology supports STA threading directly via its STA Page Handler implementation or what you might know as ASPCOMPAT mode. For WebForms running STA components is as easy as specifying the ASPCOMPAT attribute in the @Page tag:

<%@ Page Language="C#" AspCompat="true" %>

which runs the page in STA mode. Removing it runs in MTA mode. Simple.

STA for non supporting ASP.NET Technologies

only WebForms supports STA natively

  • ASP.NET HttpHandlers
  • ASMX Web Services
  • ASP.NET MVC
  • WCF Web Services
  • ASP.NET Web API

STA components are a pain in the ass. I feel your pain :-)

Good reference:

https://weblog.west-wind.com/posts/2012/Sep/18/Creating-STA-COM-compatible-ASPNET-Applications#STAfornonsupportingASP.NETTechnologies

0
Teoman shipahi On

This is a quote taken from; Pro .NET Performance

AsP.nET executes pages on MTA threads by default. If you call sTA objects, they undergo marshaling. If you predominantly call sTA objects, this will degrade performance. you can remedy this by marking pages with the ASPCOMPAT attribute, as follows:

<%@Page Language = "vb" AspCompat = "true" %>

note that page constructors still executes in an MTA thread, so defer creating sTA objects to Page_Load and Page_Init events.