Skip to content

sanosdole/nodeclrhost

Repository files navigation

nodeclrhost - Hosting .NET core in node and electron

Build status master

Latest release v8.0.0: Build status release

This project enables writing node/electron applications with .NET. This is achieved by a native node module (coreclr-hosting) that runs a .NET 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.

ElectronHostedBlazor only supports the .NET version matching the major version of this project.

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: