简体   繁体   English

从 C# .NET 调用 Node.js

[英]Calling Node.js from C# .NET

Is it possible to call Node.js functions from a C# ASP.NET server?是否可以从 C# ASP.NET 服务器调用 Node.js 函数? I found the Edge.js library, but as I understood, it allows Node.js to call into C#.我找到了 Edge.js 库,但据我所知,它允许 Node.js 调用 C#。 I need the directly reverse solution.我需要直接反向解决方案。

Edge.js has not been updated since mid 2017 and Microsoft.AspNetCore.NodeServices has been obsoleted . Edge.js 自 2017 年年中以来未更新Microsoft.AspNetCore.NodeServices 已过时

My organization maintains a library, Jering.Javascript.NodeJS , that does everything Microsoft.AspNetCore.NodeServices did and more:我的组织维护一个库Jering.Javascript.NodeJS ,它可以完成 Microsoft.AspNetCore.NodeServices 所做的一切以及更多:

Jering.Javascript.NodeJS enables you to invoke javascript in NodeJS, from C#. Jering.Javascript.NodeJS 使您能够从 C# 调用 NodeJS 中的 javascript。 With this ability, you can use javascript libraries and scripts from your C# projects.借助此功能,您可以使用 C# 项目中的 javascript 库和脚本。

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.面向 .NET Standard 2.0 和 .NET Framework 4.6.1。
    • Tested on Windows, macOS and Linux.在 Windows、macOS 和 Linux 上测试。
  • Performance features 性能特点

    • Does not start a new Node.js process for each invocation.不会为每次调用启动一个新的 Node.js 进程。 Instead, sends invocations to long-lived processes via inter-process communication.相反,通过进程间通信向长期存在的进程发送调用。
    • Optionally, runs invocations concurrently in a cluster of Node.js processes. (可选)在 Node.js 进程集群中同时运行调用。 Handles load balancing for the cluster.处理集群的负载平衡。
    • Caches compiled javascript where possible.在可能的情况下缓存已编译的 javascript。
  • Long-running application support长时间运行的应用程序支持

    • Restarts Node.js processes if they terminate unexpectedly.如果 Node.js 进程意外终止,则重新启动它们。
    • Optionally, restarts Node.js processes on file change. (可选)在文件更改时重新启动 Node.js 进程。
    • Kills Node.js processes when their parent .Net process dies.当它们的父 .Net 进程终止时杀死 Node.js 进程。
  • Flexible API灵活的API

    • Exposes both a static API and a dependency injection based API.公开静态 API和基于依赖注入的 API。
    • Supports invoking javascript in string form, Stream form, or from a file on disk.支持以string形式、 Stream形式或从磁盘上的文件调用 javascript。

Microsoft.AspNetCore.NodeServices

  • This provides a fast and robust way for .NET code to run JavaScript on the server inside a Node.js environment.这为 .NET 代码在 Node.js 环境中的服务器上运行 JavaScript 提供了一种快速而健壮的方式。 You can use this to consume arbitrary functionality from NPM packages at runtime in your ASP.NET Core app.您可以使用它在 ASP.NET Core 应用程序中在运行时使用来自 NPM 包的任意功能。

  • 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.大多数应用程序开发人员不需要直接使用它,但如果您想实现自己的功能(涉及在运行时从 .NET 调用 Node.js 代码),则可以这样做。

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

只需使用“ express”在您的节点应用中公开一个http方法,然后使用“ RestSharp” /“ HttpClient”调用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM