edge.js架起node.js和.net互操作桥梁
2013-03-31 22:23
375 查看
edge.js架起node.js和.net互操作桥梁
今天要介绍的是edge.js这个github上刚兴起的开源项目,它可以让node.js和.net之间在in-process下互操作。.net版本在4.5及以上,因为.net4.5带来的Task,asyn,await关键字和node.js的Event模型正好匹配。如果你感兴趣的话,可以参见githubhttps://github.com/tjanczuk/edge 和Edge.js overview.下面这幅图展示了edge.js在node.js和.net之间互操作的桥梁。Fun<object,Task<object>>表示输入为object类型,输出为Task<object>,后者对应node.js中的回调函数,前者则为.net方法输入参数。更多详情请参见github readme。
下面我们写个菲波基数作为demo尝鲜:完整项目寄宿在github:edge-demo。
1 var edge = require('edge'); 2 3 var fib = edge.func({ 4 source: function() {/* 5 6 using System; 7 using System.Linq; 8 using System.Threading.Tasks; 9 10 public class Startup 11 { 12 public async Task<object> Invoke(object input) 13 { 14 int v = (int)input; 15 var fib = Fix<int, int>(f => x => x <= 1 ? 1 : f(x - 1) + f(x - 2)); 16 return fib(v); 17 } 18 19 static Func<T, TResult> Fix<T, TResult>(Func<Func<T, TResult>, Func<T, TResult>> f) 20 { 21 return x => f(Fix(f))(x); 22 } 23 24 static Func<T1, T2, TResult> Fix<T1, T2, TResult>(Func<Func<T1, T2, TResult>, Func<T1, T2, TResult>> f) 25 { 26 return (x, y) => f(Fix(f))(x, y); 27 } 28 } 29 30 */}, 31 references: ['System.Core.dll'] 32 }); 33 34 fib(5, function (error, result) { 35 if (error) console.log(error); 36 console.log(result); 37 }); 38 39 var fibFromFile = edge.func(__dirname + "/fib.cs"); 40 fibFromFile(5, function (error, result) { 41 if (error) console.log(error); 42 console.log(result); 43 }); 44 45 var fibFromDll = edge.func({ 46 assemblyFile: 'edge.demo.dll', 47 typeName: 'edge.demo.Startup', 48 methodName: 'Invoke' 49 }); 50 fibFromDll(5, function (error, result) { 51 if (error) console.log(error); 52 console.log(result); 53 });
效果:
这里分为3类调用,直接源码嵌入node.js和文件外置,以后编译后的dll,多的不用说,其实很简单,如果你和一样同样喜欢js和.net的话。
在当下node.js刚兴起,成型的框架还不够多,或者有时我们必须以c或者c++来完成node.js的本地扩展的时候,edge.js给我们提供了另一个可选的途径就是 强大的.net大家族。
相关文章推荐
- edge.js架起node.js和.net互操作桥梁
- edge.js架起node.js和.net互操作桥梁 推荐
- edge.js架起node.js和.net互操作桥梁
- node.js通过edge访问.net动态链接库
- Edge.js:让.NET和Node.js代码比翼齐飞
- Edge.js:让.NET和Node.js代码比翼齐飞
- 好厉害的库edge.js!实现C#与node.js互操作
- 转载11:(译)Edge.js:让.NET和Node.js代码比翼齐飞
- Edge.js:让.NET和Node.js代码比翼齐飞(http://www.infoq.com/cn/articles/the_edge_of_net_and_node)
- Node.js操作SQLite3
- Node.js读写中文内容文件操作
- Node.js中path模块对路径的操作
- nodejs+mongoose操作mongodb副本集实例
- Node.js 学习笔记(1) 文件操作
- Node.js使用Mongoose包操作MongoDB数据库
- (NodeJS+unit.js+MochaJS+istanbul)简单操作实例
- node.js下mongoose简单操作实例
- nodejs mongoose 操作mongodb 数据库封装
- node.js使用mongodb进行删除操作和修改操作
- 【原创】.Net 微信 JS-SDK图片、语音上传接口的实现(MVC)-(二 、上传图片服务器操作)