简体   繁体   English

使用4.0框架和Visual Studio 2010为ASP Classic构建COM互操作库

[英]Building a COM interop library for ASP Classic using 4.0 framework and Visual Studio 2010

After going through a number of different articles and not finding anything especially conclusive that takes me step-by-step through the process, I've come seeking help. 在浏览了许多不同的文章之后,并没有找到需要我逐步完成过程的特别结论性的内容,我便寻求帮助。

The Scenario 场景

A client of mine is only proficient in development for ASP Classic . 我的客户仅精通ASP Classic的开发。 They have recently acquired an account for a site originally written in ASP.NET. 他们最近为最初用ASP.NET编写的网站获得了一个帐户。 They are rolling the site into something they can actively maintain, but the site originally included an image handler that took dynamically changing data regarding water levels and outputs an image containing a graphical representation of that data. 他们正在将该站点滚动到可以主动维护的位置,但是该站点最初包含一个图像处理程序,该图像处理程序获取有关水位的动态变化数据并输出包含该数据图形表示的图像。 The requirement is to develop a COM interop library that can be registered on the server and called with CreateObject to generate the same image's byte array for output using Response.BinaryWrite . 要求是开发一个COM互操作库,该库可以在服务器上注册并用CreateObject调用,以生成相同图像的字节数组,以使用Response.BinaryWrite进行输出。 The COM interop library must be registered at the remote site on a Windows 2000 Server, and I can't make any assumptions about their having access to regasm/gacutil to accomplish that task. 必须在Windows 2000 Server的远程站点上注册COM互操作库,并且我不能对它们可以使用regasm / gacutil来完成该任务进行任何假设。

The Difficulty 难点

I've built the class library by creating a Class Library project in Visual Studio 2010, choosing "COM Class" from the template, and inserting my code to generate a class with a single public method to return a byte array when given an integer (well, enumerator, but all the same). 我通过在Visual Studio 2010中创建一个类库项目,从模板中选择“ COM类”,然后插入我的代码以使用单个公共方法生成一个类,以在给定整数时返回字节数组来生成类,从而构建了类库。好,枚举器,但都一样)。 Unfortunately, even on my own development machine after building the library and registering (regasm) and caching the assembly (gacutil), I can't make a call through Classic ASP to instantiate the object, receiving instead an " ActiveX component can't create object" error. 不幸的是,即使在构建库并注册(重排)并缓存了程序集(gacutil)之后,即使在我自己的开发计算机上,我也无法通过Classic ASP进行调用以实例化对象,而是收到了“ ActiveX组件无法创建对象”错误。 And, of course, at the server site, the DLL file can't be registered, the response being "Required module was not found." 并且,当然,在服务器站点上,无法注册DLL文件,响应为“未找到所需的模块”。

Resources I've Used 我用过的资源

I've already had a look through the following articles and haven't turned up the answers I need: 我已经看过以下文章,但没有找到我需要的答案:

  1. (Basic steps) Walkthrough: Creating COM Objects with Visual Basic (基本步骤) 演练:使用Visual Basic创建COM对象
  2. Build and Deploy a .NET COM Assembly 生成和部署.NET COM程序集
  3. .NET COM+ Interop Component with Classic ASP 带有经典ASP的.NET COM + Interop组件

What I Need 我需要的

Essentially what I need is a bit of hand-holding on a kind of step by step of what it's going to take to meet the requirements and create a COM+ interop module correctly in Visual Studio 2010. Creating the actual class object itself isn't terribly difficult. 从本质上讲,我需要逐步掌握满足要求并在Visual Studio 2010中正确创建COM +互操作模块所需的步骤。创建实际的类对象本身并不十分困难难。

However, none of the articles I've looked through really discuss project options or build procedures with Visual Studio 2010 or the .NET 4.0 Framework, nor have any of them really discussed if there are special considerations for deploying to older systems like Windows Server 2000 and the actual registration of the library on a system with only, say, regsvr32 on hand. 但是,我浏览过的所有文章都没有真正讨论过使用Visual Studio 2010或.NET 4.0 Framework的项目选项或构建过程,也没有真正讨论过其中是否存在部署到Windows Server 2000之类的较旧系统时的特殊考虑。以及该库在仅使用regsvr32的系统上的实际注册。

It should be fairly straightforward to get a basic .NET assembly exposed to COM - I've never tried the COM Class project template, so this is the way I've managed it in the past: 将基本的.NET程序集公开给COM应该很简单-我从未尝试过COM Class项目模板,所以这是我过去管理它的方式:

Create a new (bog standard) .NET class library using C# or VB. 使用C#或VB创建一个新的(沼泽标准).NET类库。 Define a COM interface (replace GUIDs with your own): 定义一个COM接口(用您自己的GUID代替):

[ComVisible(true)]
[Guid("8999F93E-52F6-4E29-BA64-0ADC22A1FB11")]
public interface IComm
{
    string GetMyGroups();
}

Now define a class that implements that interface (again, replace GUIDs with your own): 现在定义一个实现该接口的类(同样,用您自己的GUID代替):

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[GuidAttribute("C5C5A1A8-9BFB-4CE5-B42C-4E6688F6840B")]
[ProgId("Test.Comm.1")]
public class Comm : IComm
{
    public string GetMyGroups()
    {
        var comm = new CommunicatorAPI.MessengerClass();

        var groups = comm.MyGroups as IMessengerGroups;
        return string.Join(", ", groups.OfType<IMessengerGroup>().Select(g => g.Name).ToArray());
    }
}

The Prog ID attribute on this class is what you will use to instantiate your component from ASP. 此类的Prog ID属性是从ASP实例化组件所使用的属性。

Strongly-name the assembly (Project properties -> "Signing" tab -> "Sign the assembly" -> Create a new strong name key file using the dropdown) 对程序集进行强命名(项目属性->“签名”选项卡->“对程序集签名”->使用下拉列表创建新的强名称密钥文件)

Now, build the assembly, and register using Regasm - if you don't wish to register in the GAC (which i'd recommend, as not GACing keeps the deployment simpler), be sure to use the -Codebase parameter (this just adds a reg entry that tells clients where to find the assembly) - eg: 现在,构建程序集,并使用Regasm注册-如果您不想在GAC中注册(我建议这样做,因为GACing不会使部署更简单),请确保使用-Co​​debase参数(这只是添加了告诉客户在哪里找到程序集的reg条目)-例如:

regasm ClassLibrary2.dll /codebase "S:\Testing\ClassLibrary2\ClassLibrary2\bin\Debug\ClassLibrary2.dll"

Now you should be able to instantiate the component, and call methods on it - for example (in javascript): 现在,您应该能够实例化该组件,并在其上调用方法-例如(在javascript中):

var a = new ActiveXObject("Test.Comm.1");
alert(a.GetMyGroups());

When it comes to deployment, the important work that Regasm and Regsvr32 do is to write various settings into the registry, so that clients can find the COM component (based on Prog ID, or COM Class ID). 关于部署,Regasm和Regsvr32要做的重要工作是将各种设置写入注册表,以便客户端可以找到COM组件(基于Prog ID或COM Class ID)。 All you need to do is work out what COM settings are being written when you run Regasm on your local machine, and write these to the registry on the server. 您需要做的就是弄清楚在本地计算机上运行Regasm时要写入的COM设置,并将其写入服务器上的注册表。 You can use ProcMon to monitor what gets written to the registry when Regasm is run. 您可以使用ProcMon监视运行Regasm时写入注册表的内容。

Generally speaking, you can expect to see something like this written to the registry: 一般来说,您可以期望看到类似以下内容的内容写入注册表:

[HKEY_CLASSES_ROOT\Test.Comm.1]
@="ClassLibrary2.Comm"

[HKEY_CLASSES_ROOT\Test.Comm.1\CLSID]
@="{00585504-90C8-4760-A359-67CAF08FFED1}"

[HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{00585504-90C8-4760-A359-67CAF08FFED1}]
@="ClassLibrary2.Comm"

[HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{00585504-90C8-4760-A359-67CAF08FFED1}\Implemented Categories]

[HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{00585504-90C8-4760-A359-67CAF08FFED1}\Implemented Categories\{62C8FE65-4EBB-45e7-B440-6E39B2CDBF29}]

[HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{00585504-90C8-4760-A359-67CAF08FFED1}\InprocServer32]
@="mscoree.dll"
"ThreadingModel"="Both"
"Class"="ClassLibrary2.Comm"
"Assembly"="ClassLibrary2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cf55d4e60653257a"
"RuntimeVersion"="v4.0.30319"
"CodeBase"="file:///S:/Testing/ClassLibrary2/ClassLibrary2/bin/Debug/ClassLibrary2.DLL"

[HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{00585504-90C8-4760-A359-67CAF08FFED1}\InprocServer32\1.0.0.0]
"Class"="ClassLibrary2.Comm"
"Assembly"="ClassLibrary2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cf55d4e60653257a"
"RuntimeVersion"="v4.0.30319"
"CodeBase"="file:///S:/Testing/ClassLibrary2/ClassLibrary2/bin/Debug/ClassLibrary2.DLL"

[HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{00585504-90C8-4760-A359-67CAF08FFED1}\ProgId]
@="Test.Comm.1"

Hope this helps :) 希望这可以帮助 :)

I have a Classic ASP web site that uses a VB6 COM object. 我有一个使用VB6 COM对象的Classic ASP网站。 I wanted to create a new version of the COM object using .NET instead of VB6. 我想使用.NET而不是VB6创建COM对象的新版本。 This is how I did it (hope this helps). 这就是我的方法(希望有帮助)。 I include instructions for both C# and VB.NET. 我提供了有关C#和VB.NET的说明。

[01] [01]

  • Start Visual Studio 2015 (run as admin). 启动Visual Studio 2015(以管理员身份运行)。
  • Create a new "Class Library" project. 创建一个新的“类库”项目。
  • Name it: "DotNetCom" 命名为“ DotNetCom”

[02] C# [02] C#

  • Add a new class, name it "HelloCOM". 添加一个新类,将其命名为“ HelloCOM”。
  • Use the following code as starting template 使用以下代码作为开始模板

( visit https://msdn.microsoft.com/en-us/library/c3fd4a20.aspx for more info ) (有关更多信息,请访问https://msdn.microsoft.com/en-us/library/c3fd4a20.aspx

    using System.Runtime.InteropServices;

    namespace DotNetCom
    {
        [Guid("EAA4976A-45C3-4BC5-BC0B-E474F4C3C83F")]
        public interface HelloCOMInterface
        {
            [DispId(1)]
            string Hello();
        }

        [Guid("7BD20046-DF8C-44A6-8F6B-687FAA26FA71"), 
            InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
        public interface HelloCOMEvents 
        {
        }

        [Guid("0D53A3E8-E51A-49C7-944E-E72A2064F938"),
            ClassInterface(ClassInterfaceType.None),
            ComSourceInterfaces(typeof(HelloCOMEvents))]
        public class HelloCOM : HelloCOMInterface
        {
            public string Hello()
            {
                return "Hello there!";
            }
        }
    }

[02] VB.NET [02] VB.NET

  • Add a new "COM class", name it "HelloCOM". 添加一个新的“ COM类”,将其命名为“ HelloCOM”。
  • VB.NET creates the starting template. VB.NET创建启动模板。
  • Add the following function to the "HelloCOM" class. 将以下函数添加到“ HelloCOM”类。

     Public Function Hello() As String Return "Hello there!" End Function 

[03] C# [03] C#

  • Open the project properties. 打开项目属性。
  • Go to "Application". 转到“应用程序”。
  • Click "Assembly Information...". 单击“组装信息...”。
  • Check "Make assembly COM-Visible" 选中“使装配COM可见”
  • Go to "Build". 转到“构建”。
  • Select "Platform target: x86". 选择“平台目标:x86”。
  • Check "Register COM for interop" 选中“注册COM以进行互操作”

[03] VB.NET [03] VB.NET

  • Open "MyProject". 打开“ MyProject”。
  • Go to "Compile". 转到“编译”。
  • Select "Target CPU: x86". 选择“目标CPU:x86”。

[04] [04]

  • Build the "DotNetCom.dll". 生成“ DotNetCom.dll”。

[05] [05]

  • Open a command prompt (run as admin). 打开命令提示符(以管理员身份运行)。
  • Change directory to your dll. 将目录更改为您的dll。

     cd DotNetComTest\\DotNetComTest\\TX7NGN.COM\\bin\\Debug 
  • Run RegAsm /codebase. 运行RegAsm / codebase。

     C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\RegAsm DotNetComTest.dll /codebase "C:\\DotNetComTest\\DotNetComTest\\bin\\Debug\\DotNetComTest.dll" 

[06] [06]

  • Start Component Services. 启动组件服务。
  • Add a new COM+ application. 添加一个新的COM +应用程序。
  • Name it: "DotNetCom". 将其命名为:“ DotNetCom”。
  • Open the "DotNetCom" properties. 打开“ DotNetCom”属性。
  • Go to the "Security Tab". 转到“安全选项卡”。
  • UNCHECK "Enforce access checks for this application". 取消选中“强制对此应用程序进行访问检查”。

[07] [07]

  • Add a new component. 添加一个新组件。
  • Select "DotNetComTest.tlb" (do NOT select "DotNetComTest.dll"). 选择“ DotNetComTest.tlb”(不要选择“ DotNetComTest.dll”)。

[08] [08]

  • Use the COM object from the Classic ASP page. 使用“经典ASP”页面中的COM对象。

     <% Dim HelloCOM Set HelloCOM = Server.CreateObject("DotNetCom.HelloCOM") Response.Write HelloCom.Hello %> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Visual Studio 2010上显示ASP.NET图表(框架4.0)? - How to display ASP.NET Charts (Framework 4.0) on Visual Studio 2010? 使用Visual Studio 2010进入asp.net中的类库 - Step into a class library in asp.net using visual studio 2010 如何在 IIS 7.5 上调试经典的 asp 页面 Visual Studio 2010? - How to debug classic asp page Visual Studio 2010 on IIS 7.5? .NET Framework 4.0是否作为Visual Studio 2010安装的一部分安装? - Is .NET Framework 4.0 installed as part of Visual Studio 2010 install? 如何确定缺少使用.net Framework 3.5依赖关系的Visual Studio 2010 asp / silverlight项目 - how to work out what Visual studio 2010 asp/silverlight project using .net framework 3.5 dependancies are missing 如何在Visual Studio 2010的目标框架中添加点网版本4.0? - How to add dot net version 4.0 in target framework of visual studio 2010? Visual Studio 2010 ASP.NET 4.0 WebForms路由在IDE调试模式下不起作用 - Visual Studio 2010 ASP.NET 4.0 WebForms Routing Not Working in IDE Debug Mode 向asp.net添加样式表(使用Visual Studio 2010) - Adding a stylesheet to asp.net (using Visual Studio 2010) 使用asp.net和Visual Studio 2010消费Web服务 - Consuming web service using asp.net and Visual Studio 2010 网格视图错误 Visual Studio 2010 asp.NET 框架 4. 找不到 .NET 框架提供程序 - Grid View Error Visual Studio 2010 asp.NET Framework 4. Unable to find .NET Framework Provider
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM