Using a .NET DLL in Node.js / serverside javascript

后端 未结 8 1332
南旧
南旧 2020-12-02 07:27

I have a pet project that is an online game, the entire game engine is written in C# and I would like to know if there is anyway I can call the functions of this existing as

8条回答
  •  無奈伤痛
    2020-12-02 08:14

    I've been recently faced with the same challenge (requirement to call C# code from node.js javascript). I had 1000s of lines of complex C# code that I really didn't like to port to javascript.

    I solved if as follows.

    • The relevant C# code is basically 1-2 classes in a DLL assembly
    • Defined a COM interface which is a subset of the C# class's interface and implemented that interface in the C# class. Thus, the DLL became an in-process COM server.
    • Implemented a node.js extension DLL that instantiates my C# COM class using standard Win32 COM API and routes method calls from node.js javascript to C# code using the COM interface.

    This solves the problem if one only wants to make calls in one direction. I also had the requirement to make calls from C# to javascript. This is a lot harder. One has to:

    • Implement a COM object in the node.js extension DLL (ATL helps here)
    • Pass an interface reference of this COM object to C# code (COM Interop)
    • Route calls via the COM object to V8 objects in node.js

    Maybe if I have some extra time, I might make an example project out of this.

提交回复
热议问题