ElectronCgi.DotNet
1.0.3
See the version list below for details.
dotnet add package ElectronCgi.DotNet --version 1.0.3
NuGet\Install-Package ElectronCgi.DotNet -Version 1.0.3
<PackageReference Include="ElectronCgi.DotNet" Version="1.0.3" />
paket add ElectronCgi.DotNet --version 1.0.3
#r "nuget: ElectronCgi.DotNet, 1.0.3"
// Install ElectronCgi.DotNet as a Cake Addin #addin nuget:?package=ElectronCgi.DotNet&version=1.0.3 // Install ElectronCgi.DotNet as a Cake Tool #tool nuget:?package=ElectronCgi.DotNet&version=1.0.3
Electron CGI
Electron CGI is a library that enables sending request form NodeJs and have them served by .NET.
The npm package is called electron-cgi.
The nuget package is called ElectronCgi.DotNet.
Here's an example of how you can interact with a .Net application from Node:
In NodeJs/Electron:
const { ConnectionBuilder } = require('electron-cgi');
const connection = new ConnectionBuilder()
.connectTo('dotnet', 'run', '--project', 'DotNetConsoleProjectWithElectronCgiDotNetNugetPackage')
.build();
connection.onDisconnect = () => {
console.log('Lost connection to the .Net process');
};
connection.send('greeting', 'John', (err, theGreeting) => {
if (err) {
console.log(err);
return;
}
console.log(theGreeting); // will print "Hello John!"
});
//or using promises
const theGreeting = await connection.send('greeting', 'John')
connection.close();
And in the .Net Console Application:
using ElectronCgi.DotNet;
//...
static void Main(string[] args)
{
var connection = new ConnectionBuilder()
.WithLogging()
.Build();
// expects a request named "greeting" with a string argument and returns a string
connection.On<string, string>("greeting", name =>
{
return $"Hello {name}!";
});
// wait for incoming requests
connection.Listen();
}
How does it work?
Electron CGI establishes a "connection" with an external process. That external process must be configured to accept that connection. In the example above that's what the Listen
method does.
In Node we can "send" requests (for example "greeting" with "John" as a parameter) and receive a response from the other process.
The way this communication channel is established is by using the connected process' stdin and stdout streams. This approach does not rely on staring up a web server and because of that introduces very little overhead in terms of the requests' round-trip time.
Changelog
Update version 1.0.3
Bugfix for Connection.Send(requestType, arg, responseArg => {...})
where argument type information for the response argument type was being inadvertently discarded.
Update version 1.0.2
Added the the UsingEncoding(System.Text.Encoding encoding)
method to ConnectionBuilder
, usage:
var connection = new ConnectionBuilder().UsingEncoding(System.Text.Encoding.UTF8).Build()
If you are having encoding issues with messages between Node and .NET failing because of special characters (e.g. ä,ö,ü) try to set the encoding this way in .NET.
Update version 1.0.1
Error propagation to Node.js
- An exception in a handler will be serialized and sent to Node.js (requires electron-cgi 1.0.0) and won't crash the process
Bugfixes
Update version 1.0.0
- This version was uploaded incorrectly. Skip it.
Update version 0.0.5
- Duplex: ability to send requests from both .Net and Node.js
Update version 0.0.2
- Ability to serve request concurrently (uses System.Threading.Tasks.DataFlow)
Next steps
- Add the ability to send requests form .Net to Node
- Instead of making the process fail when there's an exception in a handler, serialise the exception and "surface" it in Node
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Newtonsoft.Json (>= 12.0.1)
- Serilog.AspNetCore (>= 2.1.1)
- Serilog.Sinks.File (>= 4.0.0)
- System.Threading.Tasks.DataFlow (>= 4.9.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Bugfix for Send losing response arg type information