gpt4 book ai didi

.net - ASPX 页面中的 Aspcompat

转载 作者:行者123 更新时间:2023-12-01 14:00:55 26 4
gpt4 key购买 nike

在我的 aspx 页面中,我调用了一个使用 STA 的 COM 组件。所以我使用 aspcompat=true 参数,但我想了解其中的逻辑。它到底做了什么?据我所知,在 STA 中,假定 仅由一个线程调用。所以我应该自己提供线程安全。

如果是这样,aspcompat 参数在哪里提供线程安全性?在整个页面上,还是仅在我访问 STA 组件的地方?

最佳答案

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.

ASP.NET 中的 STA

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.

不支持 ASP.NET 技术的 STA

only WebForms supports STA natively

  • ASP.NET HttpHandlers
  • ASMX 网络服务
  • ASP.NET MVC
  • WCF 网络服务
  • ASP.NET 网络应用程序接口(interface)

STA 组件令人头疼。我感觉到你的痛苦:-)

很好的引用:

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

关于.net - ASPX 页面中的 Aspcompat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5212334/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com