COM Component Compatibility
Most COM components will work with ASP.NET. As with previous versions of ASP, you can still make late-bound calls to your components using Server.CreateObject. For an example, see the example in the topic Visual Basic Language Changes.
While late binding to components is still supported, early binding is a better choice for performance reasons. A tool named the Type Library Importer (TlbImp.exe), included with the .NET Framework SDK, converts standard COM components in DLL files to equivalent .NET Framework assemblies by building managed wrappers around the components. The converted components can be early-bound to managed code for greatly increased performance. For more information about the TlbImp conversion utility, see Exposing COM Components to the .NET Framework. For information about converting COM components to managed code, see Building COM Components for Interoperability.
Once the COM component is converted to a .NET assembly, you can import it into an ASP.NET page by placing a directive at the top of the page. For example, the following directive imports the namespace
MyNewNamespace, which was created by Tlbimp.exe.
The assembly file generated by Tlbimp.exe must be placed in the ASP.NET application's \Bin directory. The original COM component file must be registered for the directory in which it resides.
When using single-threaded (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. For example:
<%@Page aspcompat=true Language = VB%>
The aspcompat attribute forces the page to execute in STA mode. The runtime throws an exception if the compatibility tag is omitted and an STA component is referenced on the page. If you convert the STA component to an assembly using TlbImp, the runtime does not detect that the component uses the STA model and does not throw an exception, but your application can suffer from poor performance.
Important: COM components that are created at construction time run before the request is scheduled to the STA thread pool and consequently run on an MTA thread. This has a substantial negative performance impact and should be avoided. If you use aspcompat mode with STA components, you should create COM components only from the Page_Load event or later in the execution chain and not at page construction time. For example, the following member declaration creates the component at construction time.
<%@ ASPCOMPAT="TRUE" %> <script runat="server" language="VB">' The components is created at construction time. Dim comObj As MyComObject = New MyComObject() Public Sub Page_Load(Src As Objects, E As EventArgs) ' The object is first used here. comObj.DoSomething() End Sub </script>
Instead, use code like the following:
<%@ ASPCOMPAT="TRUE" %> <script runat="server" language="VB"> Public Sub Page_Load(Src As Objects, E As EventArgs) ' The component is created and used after the code is running ' on the STA thread pool. comObj = New MyObject() comObj.DoSomething() End Sub
COM+ 1.0 Services Microsoft Transaction Server (MTS) Components
Because of changes to the ASP.NET security model, you might need to change security access permissions for any existing Microsoft Transaction Server (MTS) components that you plan to use with ASP.NET applications. A common exception when calling an MTS component without the necessary security permissions is
[COMException (0x800a0046): Permission denied].
Before using an MTS component from an ASP.NET application, make the following security changes:
- In Component Services, open the Properties window for the MTS application.
- Click the Identity tab and change the account under which the component runs to that of a new local machine account created solely for this purpose.
- Run Dcomcnfg.exe.
- Select the Default Security tab.
- Under Default Access Permissions, click Edit Default and add the user created in step 2.
- Restart IIS to ensure that the changes are recognized.