Skip to content

syedadeel2/nodeclrhost

 
 

Repository files navigation

nodeclrhost - Hosting .NET core in node and electron

Build status master License: MIT

Latest release v0.7.8: Build status release

This project enables writing node/electron applications with .NET core. This is achieved by a native node module (coreclr-hosting) that runs a .NET core application. The .NET application uses the NodeHostEnvironment library to interact with the node runtime.

Besides running .NET in node, we can also run .NET Blazor apps in an Electron renderer process without WebAssembly. This enables access to the DOM and the full .NET core framework at the same time (including full debugger support). Instructions on how to set this up can be found here.

Simple example

To run a .NET application the following JS code is required:

const coreclrHosting = require('coreclr-hosting');

var exitcode = await coreclrHosting.runCoreApp(pathToAssembly, "Hello", "world");
console.log('.NET entry point returned: ' + exitcode);

The .NET application can use the NodeHost.Instance in its entry point like this:

class Program
{
    static Task<int> Main(string[] args)
    {
        var tcs = new TaskCompletionSource<int>();
        var host = NodeHost.Instance;
        var console = host.Global.console;
        console.log($"{args[0]} {args[1]}! Starting timeout");
        host.Global.setTimeout(new Action(() =>
                                {
                                    console.log("Timeout from node");
                                    tcs.SetResult(5);
                                }),
                                1500);
        return tcs.Task;
    }
}

This application will output:

Hello world! Starting timeout
Timeout from node
.NET entry point returned: 5

Further examples

The examples folder contains examples for:

About

Hosting .NET core in node

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 51.6%
  • TypeScript 28.0%
  • C++ 14.4%
  • JavaScript 2.5%
  • C 2.5%
  • Python 0.7%
  • Shell 0.3%