简体   繁体   English

在 .NET Core 2.0 中使用 COM 对象

[英]Using COM Object in .NET Core 2.0

So, a client has a COM object written in C from 2007 that validates an encrypted session cookie.因此,客户端有一个2007 年用 C 编写COM 对象,用于验证加密的会话 cookie。 This currently works fine with older versions of .NET using the following.这目前适用于旧版本的 .NET 使用以下内容。

var validate = Activator.CreateInstance(Type.GetTypeFromProgID("Company.Validate"));

The new implementation will be entirely in C# .新的实现将完全使用 C# I've extracted validation logic to a .NETCore class library, which we in turn reference in our ASP.Net Core project.我已将验证逻辑提取到.NETCore类库中,我们依次在ASP.Net Core项目中引用该类库。

  1. I've tried 32-bit & 64-bit versions, as well as IIS settings.我尝试过 32 位和 64 位版本,以及 IIS 设置。

  2. I've decompiled the COM object and referenced that instead, it works locally, but not on the development server, where I receive the following error:我已经反编译了 COM 对象并引用了它,它在本地工作,但不在开发服务器上,我收到以下错误:

    Retrieving the COM class factory for component with CLSID {EDC43BC6-5ECD-4501-AEB3-64A17180A13D} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).由于以下错误,检索具有 CLSID {EDC43BC6-5ECD-4501-AEB3-64A17180A13D} 的组件的 COM 类工厂失败:80040154 类未注册(来自 HRESULT 的异常:0x80040154 (REGDB_E_CLASSNOTREG)。

  3. The COM is registered both locally and on the development server. COM 在本地和开发服务器上均已注册。

  4. Activator.CreateInstance does not work in either environment. Activator.CreateInstance在任一环境中都不起作用。

  5. The registered COM ID and ProgId is the same both locally and on the development server.注册的 COM ID 和 ProgId 在本地和开发服务器上都是相同的。


Q: What am I missing, or is this not going to work?问:我错过了什么,或者这不起作用? Has anyone had any success with similar challenges?有没有人在类似的挑战中取得过任何成功? Am I overlooking anything?我忽略了什么吗?

You can't use COM with .NET Core.您不能将 COM 与 .NET Core 一起使用。 COM is Windows-only and everything in .NET Core must be cross-platform. COM 仅适用于 Windows,并且 .NET Core 中的所有内容都必须是跨平台的。 You can however run your ASP.NET Core application on the full framework, and then you can use the COM library.但是,您可以在完整框架上运行 ASP.NET Core 应用程序,然后可以使用 COM 库。 In other words, running the full framework doesn't preclude you from being able to still use ASP.NET Core.换句话说,运行完整框架并不妨碍您仍然能够使用 ASP.NET Core。 You simply will only be able to deploy to Windows, because of the full framework/COM dependency.由于完整的框架/COM 依赖性,您只能部署到 Windows。 Technically, you only need to use .NET Core if you're planning to deploy to something like a Mac or Linux.从技术上讲,如果您计划部署到 Mac 或 Linux 之类的设备,则只需要使用 .NET Core。 Otherwise, just stick to what you know.否则,就坚持你所知道的。

The required registry keys were never created.从未创建所需的注册表项。 I had to manually add about 15 keys, for the Class, Interface, and Type.我不得不为 Class、Interface 和 Type 手动添加大约 15 个键。 I believe this is because the COM had never been marshaled or invoked (because that wouldn't work) so the Class ID, AppID and registry mapping's had to be entered manually.我相信这是因为 COM 从未被编组或调用(因为那不起作用),因此必须手动输入类 ID、AppID 和注册表映射。

I've run into this recently, trying to instantiate an x86 coclass.我最近遇到了这个问题,试图实例化一个 x86 coclass。 Turns out that Visual Studio starts the first dotnet.exe which appears in the system path, no matter what architecture is actually configured.事实证明,无论实际配置什么架构,Visual Studio 都会启动出现在系统路径中的第一个 dotnet.exe。 In my case, an x86_64 variant of dotnet.exe was started as a host, which in turn can't create any x86 coclass instances.就我而言,dotnet.exe 的 x86_64 变体作为主机启动,而主机又无法创建任何 x86 coclass 实例。

Try to load your project into the apropriate variant of dotnet.exe by hand and see if this helps.尝试手动将您的项目加载到 dotnet.exe 的适当变体中,看看这是否有帮助。

Edit: This issue is discussed in more detail at https://github.com/dotnet/core/issues/901编辑:在https://github.com/dotnet/core/issues/901 上更详细地讨论了这个问题

As I can't comment yet.... You can use regsvr32 MyComLibrary.dll to add all the neccessary registry keys for you.因为我还不能发表评论......您可以使用 regsvr32 MyComLibrary.dll 为您添加所有必要的注册表项。 See https://helpdeskgeek.com/how-to/register-dll-file-in-windows/请参阅https://helpdeskgeek.com/how-to/register-dll-file-in-windows/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM