C# 应用程序可以与 Node.js 代码通信吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15136952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can a C# application communicate with Node.js code?
提问by Gero
I have a C# application and a Node.js application. I would like to press a button in my C# application to send three arguments to a Node.js application/function as input. Is this possible?
我有一个 C# 应用程序和一个 Node.js 应用程序。我想在我的 C# 应用程序中按下一个按钮,将三个参数作为输入发送到 Node.js 应用程序/函数。这可能吗?
Edit:Both applications run on the same machine. The C# application would provide three arguments to the Node.js application. The Node.js application would query a web service (POST), receive some XML data, and manipulate that data. I know that I could do that task in C# too, but in this case it has to be Node.js.
编辑:两个应用程序都在同一台机器上运行。C# 应用程序将为 Node.js 应用程序提供三个参数。Node.js 应用程序将查询 Web 服务 (POST)、接收一些 XML 数据并操作该数据。我知道我也可以用 C# 完成这个任务,但在这种情况下它必须是 Node.js。
Edit #2 and solution: Right now I have chosen: 4. Your node process runs a socket server and your C# app does requests over tcp
.
编辑 #2 和解决方案:现在我选择了:4. Your node process runs a socket server and your C# app does requests over tcp
。
I will also provide a solution that seems to work:
我还将提供一个似乎有效的解决方案:
- Node.js part: http://www.hacksparrow.com/tcp-socket-programming-in-node-js.html
- C# part: http://www.codeproject.com/Articles/1415/Introduction-to-TCP-client-server-in-C
- Node.js 部分:http: //www.hacksparrow.com/tcp-socket-programming-in-node-js.html
- C#部分:http: //www.codeproject.com/Articles/1415/Introduction-to-TCP-client-server-in-C
Now you are ready to send any data from your C# application to the Node.js server.
现在您已准备好将任何数据从 C# 应用程序发送到 Node.js 服务器。
采纳答案by AndyD
Yes communication is possible like several people have pointed out in your question's comments.
是的,就像几个人在您的问题评论中指出的那样,可以进行交流。
These are (some of) the options:
这些是(部分)选项:
- Your node process runs an http server and your C# app does JSON Rest requests over http
- Your node process runs a SOAP webservice using the node-soap/strong-soapmodule
- C# app starts your node app and you do IPC by writing to the node process inputstream and read it's outputstream.
- Your node process runs a socket server and your C# app does requests over tcp.
- You use a 3rd process/server like Redis or a Message Queue
- Anything that allows you to share data like files..
- 您的节点进程运行一个 http 服务器,您的 C# 应用程序通过 http 执行 JSON Rest 请求
- 您的节点进程使用node-soap/ strong-soap模块运行 SOAP网络服务
- C# 应用程序启动您的节点应用程序,您通过写入节点进程输入流并读取它的输出流来执行 IPC。
- 您的节点进程运行一个套接字服务器,您的 C# 应用程序通过 tcp 执行请求。
- 您使用第三个进程/服务器,如 Redis 或消息队列
- 任何允许您共享文件等数据的东西。
I would recommend you go for the first option as that doesn't require you to define a language protocol to send over the "wire". The other reason would be that there is a lot of documentation available on doing Rest with C# and node.js.
我建议您选择第一个选项,因为它不需要您定义通过“线路”发送的语言协议。另一个原因是有很多关于使用 C# 和 node.js 执行 Rest 的文档。
As the client library in C# I would suggest you have a look at Restsharpas the client library if you can't use the latest version of .NET (4.5). If you can use the latest version, use HttpClientto call your Node.js restservice. For Node just use Express.
作为 C#中的客户端库,如果您不能使用最新版本的 .NET (4.5),我建议您查看Restsharp作为客户端库。如果您可以使用最新版本,请使用HttpClient调用您的 Node.js restservice。对于 Node,只需使用 Express。
Option 2 might be quick as there is good support in VS for webservices, however, I have only used node-soap as a client so can't comment on how well the node-soap webservices are with C# clients.
选项 2 可能很快,因为 VS 对 webservices 有很好的支持,但是,我只使用 node-soap 作为客户端,因此无法评论 node-soap webservices 与 C# 客户端的关系。
回答by JeremyTCD
Manually handling inter-process communication is time-consuming and the old alternative to that, Edge.js, has not been updated since mid 2017.
手动处理进程间通信非常耗时,而且旧的替代方案 Edge.js自 2017 年年中以来一直没有更新。
My organization maintains a library, Jering.Javascript.NodeJS, that allows you to call into Node.js from C#.
我的组织维护一个库Jering.Javascript.NodeJS,它允许您从 C# 调用Node.js。
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);
You can invoke any valid Node.js module, including one that performs tasks like those listed in the question: querying a web service (POST), receiving XML data, and manipulating that data.
您可以调用任何有效的 Node.js 模块,包括执行问题中列出的任务的模块:查询 Web 服务 (POST)、接收 XML 数据和操作该数据。
Highlights
强调
Cross-platform support
- Targets .NET Standard 2.0 and .NET Framework 4.6.1.
- Tested on Windows, macOS and Linux.
Performancefeatures
- 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 concurrentlyin 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.
- Exposes both a static APIand a dependency injection based API.
- Supports invoking javascript in
string
form,Stream
form, or from a file on disk.
跨平台支持
- 面向 .NET Standard 2.0 和 .NET Framework 4.6.1。
- 在 Windows、macOS 和 Linux 上测试。
性能特点
- 不会为每次调用启动新的 Node.js 进程。相反,通过进程间通信向长期存在的进程发送调用。
- (可选)在 Node.js 进程集群中同时运行调用。处理集群的负载平衡。
- 在可能的情况下缓存已编译的 javascript。
长时间运行的应用程序支持
- 如果 Node.js 进程意外终止,则重新启动它们。
- (可选)在文件更改时重新启动 Node.js 进程。
- 当它们的父 .Net 进程终止时杀死 Node.js 进程。
- 公开静态 API和基于依赖注入的 API。
- 支持以
string
表单、Stream
表单或从磁盘上的文件调用 javascript 。