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

 

转载于:https://www.cnblogs.com/sfcyyc/p/4633441.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值