Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 assembly (.dll) from a solution built using Node.JS, Socket.IO, Express etc?

The game engine itself is pretty complete; tested and robust. I am hoping there is some neat way of exposing its functionality without too much overhead.

UPDATE:

To answer my own question a little.. I have ended building my own web socket server (based on the most current web socket protocol document). It is written in C# and compiled using Mono so that it can be hosted on a Linux box running mono and therefore (with a few tweaks) I can use my existing game engine.

UPDATE 2 A project that does exactly what I was originally looking for now exists - http://tjanczuk.github.io/edge/#/

UPDATE 3 Edge.js supporting node's last versions and .net core with a new edge-js package.

Support for Node.Js 6.x, 7.x, 8.x, 9.x, 10.x, 11.x Support for .NET Core 1.0.1 - 2.x on Windows/Linux/macOS. Support for Mono runtime 4.8.x - 5.x.

Can be installed from https://www.npmjs.com/package/edge-js

like image 609
David Avatar asked Jan 11 '11 01:01

David


People also ask

Can we use DLL in node JS?

Once you have a DLL, utilizing the interface through Node. js is pretty easy (read on!). Converting a legacy C or C++ application into a DLL can be a good integration choice when automation is too cumbersome.

Is node js server-side JavaScript safe?

While JavaScript is client-side, Node, being executed server-side, presents some vulnerabilities to different threats. Moreover, even though the core of Node. js is secure, the use of third-party components may result in additional risks.

Can you use C# with node js?

C# app starts your node app and you do IPC by writing to the node process inputstream and read it's outputstream. Your node process runs a socket server and your C# app does requests over tcp. You use a 3rd process/server like Redis or a Message Queue. Anything that allows you to share data like files..

What is node js server-side JavaScript used for?

Node. js is a runtime environment to allow JavaScript to not only be run in the browser, but also on the server (or almost any environment, really). That also expanded the types of applications that could be built with the language since it wasn't tied to only the client-side anymore.


2 Answers

Check out the edge.js project I started (http://tjanczuk.github.com/edge). It provides a mechanism for running .NET and node.js code in-process. Edge.js allows you to call .NET code from node.js and node.js code from .NET. It marshals data between .NET and node.js as well as reconciles the threading models between multi-threaded CLR and single threaded V8.

Using edge.js you can access islands of pre-existing .NET code from node.js, which seems to match your scenario.

like image 185
Tomasz Janczuk Avatar answered Oct 16 '22 19:10

Tomasz Janczuk


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.

like image 43
Kyberias Avatar answered Oct 16 '22 19:10

Kyberias