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

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.


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.


.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.


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.