.net native extension for node.js

Update: node-gyp can do the manual steps below automatically when the binding.gyp file is setup properly. See this answer for this simplified procedure.


It turned out to be rather easy. After struggling with CLR hosting and getting data in and out of the host for a while, it turns out you can actually enable /clr for your node extension no problem (so far). Here's how:

  • follow the instructions on http://nodejs.org/api/addons.html to generate the project files
  • open the generated .sln in Visual Studio (I'm on VS 2010) and enable /clr in the project settings
  • now it probably won't build and you have to let the - in this case actually quite helpful - error messages guide you to the flags that conflict with /clr

The flags that I had to change to make it work:

  • disable /EHsc (C++ exceptions)
  • disable /RTC1 and /RTCsu
  • Release: change /MT to /MD
  • Debug: change /MTd to /MDd
  • Release: change /GR- to /GR

Then you can mix managed and unmanaged code like this, referencing your .net dlls.

#pragma managed

#using <managed.dll>

void callManaged()
{
    managed::Class1^ c1 = gcnew managed::Class1();
    System::String^ result = c1->Echo("hola");
    System::Console::WriteLine("It works: " + result);
}

#pragma unmanaged

Handle<Value> Method(const Arguments& args) {
  HandleScope scope;
  callManaged();
  return scope.Close(String::New("world"));
}

Update Just discovered this link with an easy howto: http://joseoncode.com/2012/04/10/writing-your-first-native-module-for-node-dot-js-on-windows/


Sounds like edge.js is the new answer from the author of iisnode:

Edge.js supports using C# and .NET instead of writing native node.js extensions