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.
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
The examples folder contains examples for: