Run node.js and .NET code in-process

Hero Image

The owin project allows running node.js and .NET code in-process. It provides an asynchronous mechanism for calling .NET code from node.js and node.js code from .NET. The owin module takes care of marshaling the data between V8 and CLR and reconciling the threading models.

With owin you can:

  1. Use 24,000+ npm modules for node.js and 11,000+ nuget packages and .NET Framework within a single application.
  2. Combine the benefits of single-threaded node.js and multi-threaded CLR to run applications composed of IO-bound workloads and CPU-bound computations in-process.
  3. Write node.js extensions in C# and .NET Framework instead of C/C++/Win32.
  4. Use excellent CLR debugging tools (e.g. Visual Studio) to debug .NET code in your application.

Owin is based on a prescriptive pattern of a fully asynchronous interop interface shown below. It combines the essential aspects of event-based, async node.js programming model with the modern, TPL based async model that .NET offers:

clr2v8-2

Check out the owin project on GitHub for in-depth description of the features it offers. Below is simple example that illustrates the gist of the idea.

Hello, world

You need Windows, node.js v0.8.x (tested with 0.8.19), and .NET Framework 4.5 on the machine.

Implemenent a .NET function to be called from node.js in Startup.cs as follows:

   using System.Threading.Tasks;  
  
namespace Owin.Sample  
{  
    public class Startup  
    {  
        public Task<object> Invoke(object input)  
        {  
            return Task.FromResult<object>(".NET welcomes " + input.ToString());  
        }  
    }  
}
  

Compile it to Owin.Sample.dll with:

csc /target:library /out:Owin.Sample.dll Startup.cs
  

Install owin:

npm install owin
  

Implement the node.js application that will call into the .NET code in server.js:

var owin = require('owin');  
  
var helloWorld = owin.func('Owin.Sample.dll');  
  
helloWorld('JavaScript', function (error, result) {  
    if (error) throw error;  
    console.log(result);  
});
  

Run the node.js application and enjoy the response generated by .NET code displayed to the console from the node.js callback function:

C:\projects\barebones>node sample.js  
.NET welcomes JavaScript
  

The sample shows calling a .NET function from node.js, passing a string parameter to it, and receiving a string result via a callback in node.js.

More

Visit the owin project on GitHub for in-depth documentation. Contributions and derived work welcome!

Check out the previous reincarnation of the owin@0.4.0 project to get a better idea of the scenarios that informed the project in the first place.

Enjoy!


ArrowPrevious
NextArrow

Related Content

2 February 2022
fetch() In Node.js Core: Why You Should Care

Node 17.5 introduces support for the fetch() HTTP client, a new way to send requests to HTTP APIs.

23 February 2022
Node.js Adds Support for Direct Registry-less HTTPS Imports

Node is planning to introduce support for HTTPS imports in Node 18 - a feature that enables you to use urls to directly import modules over HTTPS into your project.

15 February 2022
Run Every Node.js Version in AWS Lambda

Run any version of Node.js in AWS Lambda within hours after release using custom AWS Lambda runtimes from Fusebit