简体   繁体   English

C# COM 库不能被引用

[英]C# COM library cannot be referenced

Following the guidelines from Turn a simple C# DLL into a COM interop component , I've created a tiny C# COM server:遵循Turn a simple C# DLL into a COM interop component 中的指导方针,我创建了一个小型 C# COM 服务器:

[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("410B8E64-27DB-40BD-8847-FC3A0E96147D")]
public interface IFactory
{
    int PlusOne(int i);
}

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[Guid("207FC3AB-0EFB-459A-B222-41E281F044F9")]
public class Factory : IFactory
{
    public int PlusOne(int i) => i + 1;
}

I've created as well a client C# project which just references this COM server.我还创建了一个仅引用此 COM 服务器的客户端 C# 项目。 And here is the problem: COM reference is not working, in Dependencies->COM section it is marked with a yellow warning triangle without any further explanations, and types from the COM component are not available in the client code.这就是问题所在:COM 引用不起作用,在 Dependencies->COM 部分,它标有黄色警告三角形,没有任何进一步的解释,并且来自 COM 组件的类型在客户端代码中不可用。 These projects are published on github .这些项目发布在 github 上 Note, that COM server atomaticly registers itself (regasm) upon rebuild of the project.请注意,COM 服务器会在项目重建时自动注册(regasm)。 I'll appreciate any help.我将不胜感激任何帮助。 The answers like "you can reference your server as a .net assembly directly without need to use COM", are not acceptable, as this is just a test of a part of a bigger workflow which we need to establish.诸如“您可以将您的服务器作为 .net 程序集直接引用而无需使用 COM”之类的答案是不可接受的,因为这只是我们需要建立的更大工作流的一部分的测试。

The reason for inactive reference was found after examining compiler warnings and it was in explicit prohibition for .Net client to reference .Net COM server.在检查编译器警告后找到了无效引用的原因,并且明确禁止 .Net 客户端引用 .Net COM 服务器。 There is such a possibility, though, as described here: https://docs.microsoft.com/en-us/samples/dotnet/samples/out-of-process-com-server/但是,有这种可能性,如下所述: https : //docs.microsoft.com/en-us/samples/dotnet/samples/out-of-process-com-server/

I've checked, and this really worked fine, and it became possible for a .Net client to reference a Server.Contract.tlb from the example.我已经检查过,这确实很好用,并且 .Net 客户端可以从示例中引用 Server.Contract.tlb。 As a bonus, Microsoft added the possibility for .Net Core targeting COM server to be loaded into COM Surrogate or to be run as out-of-process (standalone exe).作为奖励,微软增加了将面向 COM 服务器的 .Net Core 加载到 COM Surrogate 或作为进程外(独立 exe)运行的可能性。 This works indeed just for netcoreapp3.1 server, as when I've changed target framework to net48, no DllServer.comhost.dll (containing, most probably, proxy/stub code) was autogenerated as it did for netcoreapp3.1, the project property <EnableComHosting>true</EnableComHosting> obviously didn't have any effect.这确实仅适用于 netcoreapp3.1 服务器,因为当我将目标框架更改为 net48 时,没有像对 netcoreapp3.1 项目那样自动生成 DllServer.comhost.dll(很可能包含代理/存根代码)属性 <EnableComHosting>true</EnableComHosting> 显然没有任何效果。

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

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