在Node.js的項目中假如我們想去調用已經用C#寫的dll庫該怎么辦呢?在這種情況下Edge.js是一個不錯的選擇,Edge.js是一款在GitHub上開源的技術,它允許Node.js和.NET core在同一個進程內相互調用,並且支持Windows,MacOS和Linux。本地可以通過npm直接安裝Edge.js,地址:https://www.npmjs.com/package/edge#windows,上面有關於它的詳細介紹,里面有好多的使用情況,下文主要簡單介紹其中的一種使用方法來讓Node.js調用C#的dll庫。
1. 安裝Edge.js
npm install edge
2. Edge.js使用方法
var clrMethod = edge.func({ assemblyFile: '', //程序集dll的名稱 typeName: '', //類名,如果不指定,默認會找’Startup‘ 類 methodName: '' //方法名,方法必須是 Func<object,Task<object>> 且async ,如果不指定,默認會找'Invoke'方法});
3. 編寫C#(NodeTest.dll)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace NodeTest { public class Startup { public async Task<object> Invoke(string parameter) { var _strResult = "the input is illegal"; if (!string.IsNullOrEmpty(parameter) && parameter.Contains(",")) { var a = 0; var b = 0; if (int.TryParse(parameter.Split(',')[0], out a) && int.TryParse(parameter.Split(',')[1], out b)) { _strResult = (a + b).ToString(); } } return _strResult; } } }
4. Node.js調用dll
首先我們先編寫dotnetFunction.js文件,這個文件中我們用於加載dll
const edge = require('edge')
const path = require('path')
const fs = require('fs')
var dllPath = path.join(__dirname, 'dotnetclass/NodeTest/NodeTest/bin/Debug/NodeTest.dll')
var dotnetFunction = null
if (fs.existsSync(dllPath)) {
// 1. use defalut mode
dotnetFunction = edge.func(dllPath)
}
else {
console.log('dll path does not exist')
}
exports.add = function (parameter) {
if (dotnetFunction !== null) {
return dotnetFunction(parameter, true)
} else {
return 'dotnetFunction is null'
}
}
下面我們在nodeDotNetTest.js中來使用dotnetFunction.js中的方法
const dotnet = require('./dotnetFunction.js')
var stringAdd = '1,6'
var result = dotnet.add(stringAdd)
console.log('result : ', result)
在命令行輸入
node nodeDotNetTest.js
得到結果
result : 7
以上就是Node.js使用Edge.js調用C# dll的一個簡單例子了。但是在平時的使用中遇到的情況往往復雜的多,比如C#代碼往往注冊了一些事件,這些事件被觸發了以后需要通知Node.js做一些邏輯處理,這就涉及到C#調用Node.js了,在Edge.js中有C#調用js代碼的功能,但是是在C#代碼中嵌入js代碼,並沒有看到如何去調用Node中的指定方法,所以我覺得不合適,也許是我沒有看到,如果有小伙伴發現請告訴我糾正。那我采用的方法就是在C#代碼中新建一個隊列,事件被觸發了后就向這個隊列中加消息,在Node.js中我們設置一個定時器不斷的去從這個隊列中拿數據,根據拿到的數據進行分析再進行邏輯處理,下面就是這種方法的小例子,在這里調用C# dll時我會指定對應的程序集名稱、類名以及方法名。
5. 編寫C#代碼,Message類
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace NodeTest { public class Message { static Queue<string> _queue = new Queue<string>(); public async Task<object> Init(string parameter) { if (_queue != default(Queue<string>)) { for (int i = 0; i < 10; i++) { _queue.Enqueue(i.ToString()); } return "success"; } else { return "fail"; } } public async Task<object> Get(string parameter) { if (_queue.Count() > 0) { return _queue.Dequeue(); } else { return string.Empty; } } } }
6. 編寫dotnetFunction.js
const edge = require('edge')
const path = require('path')
const fs = require('fs')
var dllPath = path.join(__dirname, 'dotnetclass/NodeTest/NodeTest/bin/Debug/NodeTest.dll')
var dotnetFunction = null
var dotnetInitFunction = null
if (fs.existsSync(dllPath)) {
dotnetInitFunction = edge.func({
assemblyFile: dllPath,
typeName: 'NodeTest.Message',
methodName: 'Init'
})
dotnetFunction = edge.func({
assemblyFile: dllPath,
typeName: 'NodeTest.Message',
methodName: 'Get'
})
}
else {
console.log('dll path does not exist')
}
exports.init = function () {
if (dotnetInitFunction !== null) {
return dotnetInitFunction("", true)
} else {
return 'dotnetInitFunction is null'
}
}
exports.getmessage = function () {
if (dotnetFunction !== null) {
return dotnetFunction("", true)
} else {
return 'dotnetFunctionis null'
}
}
7. 編寫nodeDotNetTest.js
const dotnet = require('./dotnetFunction.js')
var initresult = dotnet.init()
console.log('init result : ', initresult)
var getmessage = function () {
var message = dotnet.getmessage()
if (message != undefined && message !== null && message !== '') {
console.log('message : ', message)
}
}
setInterval(getmessage, 100)
8. 命令行輸入
node nodeDotNetTest.js
得到結果
init result : success message : 0 message : 1 message : 2 message : 3 message : 4 message : 5 message : 6 message : 7 message : 8 message : 9
可以看到,完全可以從隊列中取到消息,只要能拿到消息,我們的在Node.js中就能做對應的處理。以上就是關於Edge.js的一些使用方法,希望能夠幫到大家!
