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,网上资源很多

 

posted @ 2015-07-09 16:01  -下午茶-  阅读(3457)  评论(1编辑  收藏  举报