61

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

3
  • @Dve: I'm in pretty much the same boat as you now. Was it difficult to write your own web socket server? I'm debating if I should try that as well, or look at existing C# web servers. More importantly though, what did you end up doing client side? Did you just sent ajax requests to your custom server, or did you continue to use node.js/socket.io client-side to send the requests to your server?
    – mpen
    Commented Nov 4, 2011 at 6:37
  • @Mark the project went on hold, but is now back in action thanks to the iisnode project - github.com/tjanczuk/iisnode
    – David
    Commented Nov 4, 2011 at 9:54
  • @Dve I am on the same path. I'm still new to edge. How did you create objects and such from nodejs?
    – TomO
    Commented Jun 29, 2015 at 21:02

8 Answers 8

30
+400

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.

3
  • thanks for the useful edge nodejs module. I could use it to call ProtectedData.Unprotect api for reading secured configuration.
    – Sushil
    Commented Jun 3, 2013 at 12:04
  • I am really trying to find a working example of importing a DLL and running its methods using edge-js... any help?
    – john k
    Commented Sep 14, 2020 at 17:58
  • 7
    This project is dead, I had troubles building a version for node v15. Commented Jan 12, 2021 at 21:02
27

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.

3
  • 2
    It would be so great if you came with an example... Still, upvoted for the inspiration.
    – Ege Özcan
    Commented Jul 1, 2012 at 12:53
  • +1 - if you have time, a step by step tutorial would be Awesome. Commented Dec 27, 2012 at 16:26
  • 1
    One more here, did pretty much what you were saying, up to preparing the module, but now i am stuck. I get: Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'My file with COM interface' or one of its dependencies. The system cannot find the file specified. A snipped would be very apreciated.
    – blueomega
    Commented Mar 28, 2013 at 8:23
18
+25

If all you want to do is spin up a lightweight HTTP server while still programming with C# and .Net you should give Kayak a chance. It is a lightweight HTTP Server for C# and behaves kind of like node.js in that sense.

kayakhttp

Update:

If you are looking for a lightweight HTTP Server to handle web requests you have a couple alternatives today:

  • ServiceStack (recommended)
  • Microsoft WebAPI
  • NancyFx

To my knowledge all the above work on some version of Mono, so you can still host them across both Windows and Unix based systems.

2
  • update to this answer. You don't have to use Kayak these days. You can use WebAPI that ships with the newer version of ASP.Net. Commented Nov 27, 2012 at 19:40
  • Broken link :( Looks like domain squatters got to it. Commented Feb 20, 2013 at 6:22
8

.Net addons can be written, in short you write a regular native addon and add .Net calls via CLI/C++ calls to .Net dlls.

In practice you usually create a C# dll library which you then call from a CLI/C++ node addon project. There is a bit of delicacies such as making sure that the actual node add on definition file is compiled without CLR support so node can load it correctly.

You can check out: https://github.com/saary/node.net for an example of how this can be achieved.

4
  • 1
    Answers with just links can be unhelpful if the linked page goes away. Can you summarize it a little as the other answers do? Commented Nov 23, 2011 at 0:04
  • -1 node.net is a red herring. Check the last step: 'Change the "Common Language Runtime Support" option to No Common Language RunTime Support'. In other words, no CLR Commented Jun 29, 2012 at 6:59
  • It is most definitely not a red herring. The last step talks about the node.js addon binding file which is the only file that is compiled without CLR support (which makes total sense as node.js is native).
    – saary
    Commented Aug 3, 2012 at 21:36
  • @saary You are absolutely right. I should have read more closely. My apologies. Un-minus1! Commented Jan 7, 2013 at 4:08
6

The following answer is out of date, but still helpful for understanding of Node.js from first release
Node.js is now also available natively for Windows at nodejs.org. No cygwin requirement or otherwise.

First of all, at the moment there's no native Windows port of Node.js, there's only a cygwin version (but I suspect you already knew that).

There was a node module floating around somewhere at the GitHubs that provided wrappers for calling into native libraries, but iirc, that only worked with .so libs.

Therefore, if you want to use a C# DLL, you will first have to write a native Node.js extension as the interface:
https://www.cloudkick.com/blog/2010/aug/23/writing-nodejs-native-extensions/

From that extension you have to load the DLL and wrap the calls from Node.js to the C# code, that means you have to write some low level C/C++ code and convert C# values to V8 stuff.

I only have experience with C++ and V8, it's a bit hard to get started since the code examples are a bit sparse, also wrapping C++ classes is not that trivial. But I did wrote small JS game engine kind of thing, that uses a C++ OpenGL backend, it's unfinished (and there are hardly any comments) but it might give you some ideas.

Note: There are some projects in the wild that provide somewhat automatic generation of wrappers to V8, but those are C++ only.

So to conclude, I think it will be quite adventurous getting the C# wrappers to work, but it should be possible.

2
  • Appologies for the slow response to your answer. Thanks for the information, but as interesting as it is, it feels to messy and thats what I have been trying to avoid. The more I look at it the more I look at the project the more I wish I hadn't coded the entire game engine in C#, it seems to be limiting my options now!
    – David
    Commented Jan 18, 2011 at 15:18
  • 3
    No longer accurate. Node is native on Windows now.
    – rainabba
    Commented Jan 3, 2014 at 23:32
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 (npm i edge-js) from https://www.npmjs.com/package/edge-js

2

You might have some luck with this project, which is a port of Node.js to .NET. I haven't used it myself, but with a native .NET implementation you theoretically should be able to do what you need to.

You might also want to go the other direction and try to port (aka: recompile unless you're hooked deep into Windows) your C# game engine to Mono and see if you can then build wrappers off of that.

1
  • This is another project worth a shot, similar to the above poster but runs on V8.
    – gatapia
    Commented Jun 2, 2011 at 4:01
1

I know it's an old question, but wanted to throw in a current answer. With IIS 7.5 and .Net 4.x Websockets are supported, though use of the SignalR library will likely be the path of least resistance. It's similar to the socket.io library for NodeJS.

As to accessing .Net code via NodeJS, your best options are Edge.js, building a mixed native assembly with C/C++, exposing your .Net code either via a command line application (best to use pipes for input/output) or via a service (TCP or other).

I find Edge.js to be very limited, and not offer much over a piped console interface.. and feel that a service may be best for a more complex interface. At which point you may be best doing the rest of the project in .Net, unless you have an investment in NodeJS that supersedes said difficulties.

2
  • For what it's worth, I am a big fan of NodeJS, but tend to be very paragmatic about it.
    – Tracker1
    Commented Apr 18, 2013 at 8:48
  • Great suggestion. It's always nice to see a more recent take on an older (but still valid and relevant) question.
    – Troy Gizzi
    Commented Oct 16, 2016 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.