Can a C# application communicate with Node.js code?

Yes communication is possible like several people have pointed out in your question's comments.

These are (some of) the options:

  1. Your node process runs an http server and your C# app does JSON Rest requests over http
  2. Your node process runs a SOAP webservice using the node-soap/strong-soap module
  3. C# app starts your node app and you do IPC by writing to the node process inputstream and read it's outputstream.
  4. Your node process runs a socket server and your C# app does requests over tcp.
  5. You use a 3rd process/server like Redis or a Message Queue
  6. Anything that allows you to share data like files..

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.

As the client library in C# I would suggest you have a look at Restsharp as the client library if you can't use the latest version of .NET (4.5). If you can use the latest version, use HttpClient to call your Node.js restservice. For Node just use 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.


Manually handling inter-process communication is time-consuming and the old alternative to that, Edge.js, has not been updated since mid 2017.

My organization maintains a library, Jering.Javascript.NodeJS, that allows you to call into Node.js from C#.

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.

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.

Tags:

C#

Node.Js