25

Is it possible to call Node.js functions from a C# ASP.NET server? I found the Edge.js library, but as I understood, it allows Node.js to call into C#. I need the directly reverse solution.

Robert Claypool
  • 4,262
  • 9
  • 50
  • 61
Kamiky
  • 601
  • 2
  • 8
  • 25

2 Answers2

21

Edge.js has not been updated since mid 2017 and Microsoft.AspNetCore.NodeServices has been obsoleted.

My organization maintains a library, Jering.Javascript.NodeJS, that does everything Microsoft.AspNetCore.NodeServices did and more:

Jering.Javascript.NodeJS enables you to invoke javascript in NodeJS, from C#. With this ability, you can use javascript libraries and scripts from your C# projects.

Example usage

string javascriptModule = @"
module.exports = (callback, x, y) => {  // Module must export a function that takes a callback as its first parameter
    var result = x + y; // Your javascript logic
    callback(null /* If an error occurred, provide an error object or message */, result); // Call the callback when you're done.
}";

// Invoke javascript in Node.js
int result = await StaticNodeJSService.InvokeFromStringAsync<int>(javascriptModule, args: new object[] { 3, 5 });

// result == 8
Assert.Equal(8, result);

Highlights

  • Cross-platform support

    • Targets .NET Standard 2.0 and .NET Framework 4.6.1.
    • Tested on Windows, macOS and Linux.
  • Performance features

    • Does not start a new Node.js process for each invocation. Instead, sends invocations to long-lived processes via inter-process communication.
    • Optionally, runs invocations concurrently in a cluster of Node.js processes. Handles load balancing for the cluster.
    • Caches compiled javascript where possible.
  • Long-running application support

    • Restarts Node.js processes if they terminate unexpectedly.
    • Optionally, restarts Node.js processes on file change.
    • Kills Node.js processes when their parent .Net process dies.
  • Flexible API

    • Exposes both a static API and a dependency injection based API.
    • Supports invoking javascript in string form, Stream form, or from a file on disk.
JeremyTCD
  • 695
  • 1
  • 6
  • 11
  • Seems like Microsoft.AspNetCore.NodeServices is being actively maintained: https://www.nuget.org/packages/Microsoft.AspNetCore.NodeServices/ – triple.vee Jun 11 '20 at 07:33
  • 6
    The NodeServices Nuget package is auto-generated from the dotnet/aspnetcore repository, along with many other packages, whenever their shared version is bumped. Apart from project metadata, the package's underlying [source code has not been updated this last 10 months](https://github.com/dotnet/aspnetcore/tree/master/src/Middleware/NodeServices/src). In the repository, INodeServices [remains marked as obsolete](https://github.com/dotnet/aspnetcore/blob/master/src/Middleware/NodeServices/src/INodeServices.cs), with a "use spa extensions" warning. – JeremyTCD Jun 11 '20 at 09:20
  • Confirm obsolete by microsoft documents. – phonemyatt Jul 27 '20 at 08:03
  • I got a bit further. code: result = await StaticNodeJSService.InvokeFromStringAsync("//test_nodejs_script.js", "appendExclamationMark", args: new[] { "success" }); error: The module appendExclamationMark has no exports. Ensure that the module assigns a function or an object containing functions to module.exports. JS code: module.exports = {appendExclamationMark: (callback, message) => callback(null, { resultMessage: message + '!' })}; – Mark Jan 18 '21 at 14:49
  • Use InvokeFromFileAsync to invoke from a file. If you need more help feel free to [open an issue on Github](https://github.com/JeringTech/Javascript.NodeJS/issues/new). Easier to discuss code there. – JeremyTCD Jan 19 '21 at 02:03
  • Edge.js has a reasonably maintained fork https://github.com/agracio/edge-js (not much activity lately, but at least it works...) – exyi Jun 07 '22 at 13:37
8

Microsoft.AspNetCore.NodeServices

  • This provides a fast and robust way for .NET code to run JavaScript on the server inside a Node.js environment. You can use this to consume arbitrary functionality from NPM packages at runtime in your ASP.NET Core app.

  • Most applications developers don't need to use this directly, but you can do so if you want to implement your own functionality that involves calling Node.js code from .NET at runtime.

Source: https://github.com/aspnet/JavaScriptServices

Community
  • 1
  • 1
Robert Claypool
  • 4,262
  • 9
  • 50
  • 61