DCOM服务器和客户端.NET编写客户端、服务器、DCOM、NET

2023-09-03 06:31:13 作者:丅一秒婞諨

我正在开发的.NET 4.0(VS2010,C#),DCOM服务器。就其本身而言,这是工作的罚款。

I'm developing a DCOM server in .NET 4 (VS2010, C#). By itself, this is working fine.

现在,我还需要制定一个.NET客户端,此DCOM服务器,但我无法添加引用类型库。 Visual Studio会告诉我的类型库从.NET程序集导出,并且不能被添加作为参考。

Now, I also need to develop a .NET client for this DCOM server, but I am unable to add a reference to the TypeLib. Visual Studio will tell me the type library was exported from a .NET assembly and cannot be added as a reference.

解答this问题建议,我应该可以使用 TLBIMP.EXE 来生成一个包装程序集,但它拒绝这样做还有:

Answers to this question suggests that I should be able to use TlbImp.exe to generate a wrapper assembly, but it will refuse to do so as well:

TLBIMP:错误TI1029:类型库   MyWrapper从CLR导出   组件,并且不能被重新导入为   一个CLR程序集。

TlbImp : error TI1029 : Type library 'MyWrapper' was exported from a CLR assembly and cannot be re-imported as a CLR assembly.

据我所知,从纯粹的.NET的角度来看,它可能无法使很​​多意义,使用DCOM这一点。然而,相同的服务器也应该从非.NET应用程序进行访问。

I understand that from a purely .NET perspective, it may not make a lot of sense to use DCOM for this. However, the same server should also be accessible from non .NET applications.

我曾尝试将我的TLB到IDL和再生从该TLB,但是这并不能糊弄的Visual Studio。

I have tried converting my tlb to IDL and regenerating the tlb from that, but this does not fool Visual Studio.

也许有可能再生之前修改IDL咯,或者是有一些方法来强制使用DCOM的,即使这两个服务器和客户端都写在.NET?

Perhaps it is possible to modify the IDL slightly before regenerating, or is there some way to force the use of DCOM, even though both server and client are written in .NET?

推荐答案

我设法得到DCOM的工作,但我不能确定它是否可以从一个类型库来完成。修改IDL让我导入类型库,但在编译过程中最终未能(虽然这被视为一个警告由Visual Studio)。它可能仍然是可能使更多的修改文件,但我使用一个更容易的解决方案。

I managed to get DCOM working, but I'm not certain if it can be done from a TypeLib. Modifying the IDL allowed me to import the type library, but it eventually failed during compilation (although this is treated as a warning by Visual Studio). It might still be possible to make even more modifications to the file, but I'm using a much easier solution.

所有的DCOM服务器的接口定义被转移到一个单独的程序,然后直接从.NET客户端引用。这就避免进口问题。

All the interface definitions for the DCOM server were moved to a separate assembly, which is then referenced directly from the .NET client. This circumvents the importing problem.

然后,访问DCOM服务器没有从人们所预期的不一样:

Then, accessing the DCOM server is no different from what one might expect:

Guid clsId = new Guid("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");
Type type = Type.GetTypeFromCLSID(clsId);
IMyInterface comObject = (IMyInterface)Activator.CreateInstance(type);

移动接口到一个单独的组件不是严格必需的,但是这最大限度地减少了共享组件的尺寸。

Moving the interfaces to a separate assembly is not strictly necessary, but this minimizes the size of the shared assembly.