Edge實現NodeJS與.NET互操作(包括UI介面樣本)
Edge實現NodeJS與.NET互操作
Kimmking@163.com 2015-01-14
1、 Edge是什麼
Edge是一種在進程內實現NodeJS與.NET互操作的橋接技術,可以在NodeJS裡使用.NET代碼和庫,也可以在.NET程式裡使用NodeJS的代碼。
Edge運行需要.netframework4.5,它使用.NET的Task、async、await機制跟NodeJS的event模型匹配。本質上是串連V8引擎和.NET /monoCLR運行時,同時支援Windows、MacOS、Linux。同時它還支援運行於.NET CLR上的各種指令碼語言。藉由這種進程內的橋接技術,兩邊的各種類庫和其他技術就可以互連有無了,例如NodeJS使用.NET的影像處理庫GDI+、直接用ADO.NET操作SQLServer資料庫,甚至直接調用Winform的代碼實現案頭UI程式等等。
Edge由微軟的技術人員Tomasz Janczuk建立於2013年2月。
安裝Edge很簡單,只需要npm install –gd edge即可。
一個最簡單的例子hello.js(NodeJS裡使用C#代碼):
var edge=require('edge');
varhelloWorld= edge.func(function () {/*
async (input) => { //這裡是C#代碼
return .NET Welcomes + input.ToString();
}
*/});
helloWorld('JavaScript',function (error, result) {
if (error) throw error;
console.log(result);
});
執行node hello.js時,會先調用.net framework編譯/* */內的C#代碼。然後執行整個混編的程式,整個過程只有一個node.exe的進程。
或另一種用法(C#裡使用NodeJS代碼):
using System;
using System.Threading.Tasks;
using EdgeJs;
classProgram
{
publicstaticasyncvoidStart()
{
var func = Edge.Func(@
return function (data, callback) {
callback(null, 'Node.js welcomes ' + data);
}
);
Console.WriteLine(await func(.NET));
}
staticvoidMain(string[] args)
{
Task.Run((Action)Start).Wait();
}
}
更多資訊參見:Edge.js overview
2、 Edge能做什麼
除了上面例子提到的NodeJS與C#簡單調用對方的代碼實現,Edge還可以實現更複雜的功能,
2.1資料和函數傳遞
例如從NodeJS傳遞資料到.NET中去:
var dotNetFunction = edge.func('Edge.Sample.dll');
var payload = {
anInteger:1,
aNumber:3.1415,
aString:'foo',
aBoolean:true,
aBuffer:newBuffer(10),
anArray: [ 1, 'foo' ],
anObject: { a:'foo', b:12 }
};
dotNetFunction(payload, function (error, result) { });
直接把資料和函數傳入C#,讓C#回調NodeJS的函數:
var edge =require('edge');
var addAndMultiplyBy2 = edge.func(function () {/*
async (dynamic input) => {
var add = (Func>)input.add;
var twoNumbers = new { a = (int)input.a, b = (int)input.b };
var addResult = (int)await add(twoNumbers);
return addResult * 2;
}
*/});
var payload = {
a:2,
b:3,
add: function (data, callback) {
callback(null, data.a + data.b);
}
};
addAndMultiplyBy2(payload, function (error, result) {
if (error) throw error;
console.log(result);
});
需要注意的一點,為了防止進程內阻塞NodeJS的事件機制,NodeJS裡無法直接調用.NET的方法,必須用Func>封裝成非同步回調方式。
2.2 .NET引用NodeJS的第三方庫
classProgram
{
publicstaticasyncvoidStart()
{
var createWebSocketServer = Edge.Func(@
var WebSocketServer = require('ws').Server;
return function (port, cb) {
var wss = new WebSocketServer({ port: port });
wss.on('connection', function (ws) {
ws.on('message', function (message) {
ws.send(message.toUpperCase());
});
ws.send('Hello!');
});
cb();
};
);
await createWebSocketServer(8080);
}
staticvoidMain(string[] args)
{
Task.Run((Action)Start);
new ManualResetEvent(false).WaitOne();
}
}
簡簡單單,So easy!
2.3 ASP.NET裡使用NodeJS代碼
只需要用NuGet 安裝Edge.JS,然後把node_modules複製到ASP.NET的webapplication裡的bin目錄即可。
2.4 NodeJS中使用基於.NET CLR的指令碼語言
以python為例,3個步驟:
1) 安裝依賴
npm install edge
npm install edge-py
2) 寫混編代碼
var edge =require('edge');
var hello = edge.func('py', function () {/*
def hello(input):
return Python welcomes + input
lambda x: hello(x)
*/});
hello('Node.js', function (error, result) {
if (error) throw error;
console.log(result);
});
3) 執行
$>node py.js
Python welcomes Node.js
2.5 NodeJS中使用C#建立Winform案頭UI程式
NodeJS程式hello.js如下:
var edge = require('edge'); var hello = edge.func(function () {/* #rSystem.Data.dll #rSystem.Windows.Forms.dll #rSystem.Drawing.dll using System.Data; using System.Threading.Tasks; usingSystem.Windows.Forms; usingSystem.ComponentModel; usingSystem.Drawing; async(input) => { Formf = new Form(); f.Text = 大漠窮秋; Label l = new Label(); l.SetBounds(10, 10, 150, 20); l.Text = 飛豬; f.Controls.Add(l); TextBox t = new TextBox(); t.SetBounds(10, 35, 150, 20); t.Text = 理工男;f.Controls.Add(t); ComboBox c = new ComboBox(); c.SetBounds(10, 60, 150, 20); c.Text =翟偉; f.Controls.Add(c); Button b = new Button(); b.SetBounds(10, 85, 150, 30); b.Text = 棒棒糖;f.Controls.Add(b); b.Click += (oo, ee) => { MessageBox.Show(this applicationcreated by KimmKing, 邊邊says); }; l.BackColor = Color.Green; l.ForeColor = Color.Tomato; f.ShowDialog(); return.NET welcomes + input.ToString(); }*/}); hello('Node.js', function (error, result) { if(error) throw error; console.log(result);});
執行node hello.js,效果如下(官方沒有這個方面的demo):
3、總結&&個人看法
Edge作為一個橋樑,打通了兩個平台的任督二脈,基本上兩邊原有的東西,都可以相互調用,極大的增強了兩個體系的功能拓展。
缺點也很明顯:
1、效能:拿NodeJS調用C#來說,比原生的NodeJS差不少,see:Edge效能
2、融合:因為是橋接技術,其實還是兩個完整獨立的體系,無法細粒度的融合,比如直接用js調用winform的組件實現一套NodeJS的UI技術(如果要實現這一點,還需要一個複雜的中介層)。