COM 组件的兼容性

更新:2007 年 11 月

大多数 COM 组件都能够与 ASP.NET 一起工作。与 Active Server Pages (ASP) 的以前版本一样,您仍然可以使用 CreateObject 函数 (Visual Basic) 对组件执行后期绑定调用。有关更多信息,请参见 .NET Framework 应用程序中的 COM 互操作性

本主题包含以下部分:

  • 早期绑定

  • 64 位组件

早期绑定

虽然仍支持后期绑定到组件,但是基于性能的考虑,早期绑定是一个更好的选择。Windows 软件开发工具包 (SDK) 包含一个名为 类型库导入程序 (Tlbimp.exe) 的工具,该工具通过生成组件的托管包装来将 .dll 文件中的标准 COM 组件转换为等效的 .NET Framework 程序集。转换后的组件可以早期绑定到托管代码,这样将大大提高性能。有关 Tlbimp.exe 的更多信息,请参见向 .NET Framework 公开 COM 组件。有关将 COM 组件转换为托管代码的信息,请参见为交互操作生成 COM 组件

将 COM 组件转换为 .NET Framework 程序集后,可以通过在 ASP.NET 页的顶部放一条指令将它导入到该页。例如,下面的指令导入由 Tlbimp.exe 创建的命名空间 MyNewNamespace。

<%@Import Namespace="MyNewNamespace"%>

由 Tlbimp.exe 生成的程序集文件必须放在 ASP.NET 应用程序的 Bin 目录下。原始的 COM 组件文件必须对它所驻留的目录进行注册。

从 ASP.NET 页中使用单线程单元 (STA) COM 组件(例如,使用 Visual Basic 开发的组件)时,必须在该 ASP.NET 页上的 <%@ Page > 标记中包括兼容性属性 AspCompat=true,如下面的代码示例所示。

<%@Page AspCompat=true Language = VB%>

AspCompat 属性强制该页在 STA 模式下执行。如果省略了兼容性标记并且在该页上引用了一个 STA 组件,则运行库将引发异常。如果使用 Tlbimp.exe 将 STA 组件转换为程序集,则运行库不会检测到该组件使用了 STA 模型且不会引发异常,但应用程序的性能将会很差。

zwk9h2kb.alert_caution(zh-cn,VS.90).gif重要说明:

构造时创建的 COM 组件在预定对 STA 线程池进行请求之前运行,因此在多线程单元 (MTA) 线程上运行。这会对性能有严重的负面影响,应避免这样做。如果将 AspCompat 用于 STA 组件,则只应从 Page_Load 事件创建 COM 组件,或以后在执行链中创建 COM 组件,而不应在构造页时创建该组件。

例如,以下成员声明在构造时创建组件。

<%@ 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>
<%@ 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>

应使用如下所示的代码:

<%@ 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>
<%@ 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 位组件

在 64 位版本的 Microsoft Windows 上,可以使用 WOW64 仿真程序运行 32 位应用程序。但进程只能是 32 位或 64 位。您不能让某个进程既运行于 32 位又运行于 64 位。

Internet 信息服务 (IIS) 在 64 位版本的 Windows 上作为 64 位应用程序运行。COM 组件的进程类型必须与 IIS 辅助进程类型匹配。使用下面一种解决方案可以在 64 位版本的 IIS 上运行 32 位组件:

建议您将组件转换为 64 位。在使用 Visual Basic 组件的情况下,则不可能这样做,因为没有 64 位版本的 Visual Basic 编译器。

请参见

任务

如何:在 ASP.NET 中更新现有 MTS 组件的权限

概念

向 .NET Framework 公开 COM 组件

为交互操作生成 COM 组件

ASP.NET 网站中的共享代码文件夹