JavaScript Editor jscript editor     Web designer 



Main Page

Most COM components will work with ASP.NET. As with previous versions of Active Server Pages (ASP), you can still make late-bound calls to your components using CreateObject Function (Visual Basic). For more information, see COM Interoperability in .NET Framework Applications.

This topic contains the following sections:

Early Binding

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 Tlbimp.exe, see Exposing COM Components to the .NET Framework. For information about converting COM components to managed code, see Building COM Components for Interoperability.

After the COM component is converted to a .NET Framework 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.

В CopyCode imageCopy Code
<%@Import Namespace="MyNewNamespace"%>

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 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, as shown in the following code example.

В CopyCode imageCopy Code
<%@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.exe, 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.

NoteImportant

COM components that are created at construction time run before the request is scheduled to the STA thread pool and consequently run on a multithreaded apartment (MTA) thread. This has a substantial negative performance impact and should be avoided. If you use AspCompat 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.

C#В CopyCode imageCopy Code
<%@ Page AspCompat="true" Language="C#" %>

<script runat="server">
    // The components is created at construction time.
    MyComObject comObj = new MyComObject();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        // The object is first used here.
        comObj.DoSomething();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>
Visual BasicВ CopyCode imageCopy Code
<%@ Page AspCompat="true" Language="VB" %>

<script runat="server">
    ' The components is created at construction time.
    Dim comObj As MyComObject = New MyComObject()
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        ' The object is first used here.
        comObj.DoSomething()
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

Instead, use code like the following.

C#В CopyCode imageCopy Code
<%@ Page AspCompat="true" Language="C#" %>

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        // The component is created and used after the code is running 
        // on the STA thread pool.
        MyComObject comObj = new MyComObject();
        comObj.DoSomething();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>
Visual BasicВ CopyCode imageCopy Code
<%@ Page AspCompat="true" Language="VB" %>

<script runat="server">
    ' The component is created and used after the code is running 
    ' on the STA thread pool.
    Dim comObj As MyComObject = New MyComObject()
    comObj.DoSomething()
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

64-Bit Components

On a 64-bit version of Microsoft Windows, it is possible to run 32-bit applications using the WOW64 emulator. However, processes can only be 32-bit or 64-bit. You cannot have a process that runs both.

Internet Information Services (IIS) runs as a 64-bit application on 64-bit versions of Windows. The process type of a COM component must match the IIS worker process type. It is possible to run 32-bit components on the 64-bit version of IIS using one of the following solutions:

It is recommended that you convert your components to 64-bit. In the case of Visual Basic components, this is not possible because there is no 64-bit version of the Visual Basic compiler.

See Also

Tasks

How to: Update Permissions for Existing MTS Components in ASP.NET

Concepts

Exposing COM Components to the .NET Framework
Building COM Components for Interoperability
Shared Code Folders in ASP.NET Web Sites



JavaScript Editor jscript editor     Web designer