node.js通過edge訪問.net動態鏈接庫


從了解node.js到現在經歷了幾個月時間,一直忙於實際的項目,沒有動手寫點關於node.js的代碼。最近將開發工作安排就緒,個人的時間相對從容了,所以這幾天開始測試一下node.js。

多年來,一直在微軟的平台下開發,大量代碼都是采用C#編寫,從系統平台,到一個個的開發工具,基本上都是基於C#的,因此,如果采用node.js開發新的業務系統,我不得不最大限度地重用以前的基於c#的功能模塊,在網上搜索了一下,知道有個叫edge.js的組件,可以方便的調用.net模塊,一段時間的試驗之后,調用通過了,先將代碼貼出來:

操作系統:windows 8.1

通過C#創建一個DLL,名字為some.dll,代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Some
{
    public class Startup
    {
        public async Task<Object> Invoke(object input)
        {
            return Helper.SayHi("Invoke1:" + (string)input);
        }

        public async Task<Object> Invoke2(object input)
        {
            return Helper.SayHi("Invoke2:" + (string)input);
        }

        static class Helper
        {
            public static string SayHi(string param)
            {
                return ".NET Welcomes " + param;
            }
        }
    }
}

 

node代碼(abc.js)代碼如下:

var i=1;
console.info(i);
console.log("Hello edge");
console.log("Hello Node JS");

var edge = require('edge');
console.info("call c#")
var helloWorld = edge.func('async (input) => { return ".NET Welcomes " + input.ToString(); }');
helloWorld("Node.js", function (err, result) 
{
    if (err) throw err;   
    console.log(result);
});

var invoke1=edge.func({
    assemblyFile:"Some.Dll",
    typeName:"Some.Startup",
    methodName: "Invoke"
})

invoke1("Call .net method from DLL",function(err,result)
{
    if (err) throw err;   
    console.log(result);
});

var invoke2=edge.func({
    assemblyFile:"Some.Dll",
    typeName:"Some.Startup",
    methodName: "Invoke2"
})

invoke2("Call .net method from DLL",function(err,result)
{
    if (err) throw err;   
    console.log(result);
});

 

注意,將some.dll和abc.js放在同一個目錄下,然后,在命令窗口下運行node app.js即可顯示如下結果:

關於npm安裝edge.js,網上資源很多

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM