2016-10-20 EdgeJS, C#<=>node.js

来源:互联网 发布:java并行程序设计 pdf 编辑:程序博客网 时间:2024/04/28 00:49


花了一个多小时研究为什么C#调用jsfunctionwork。这是梳理过程。

1 script Node.js from C# works.

publicstaticasyncTask Start()

       {

           var func =Edge.Func(@"

           returnfunction (data, callback) {

               callback(null, 'Node.js welcomes ' + data);

           }

       ");

 

           Console.WriteLine(await func(".NET"));

       }

       staticvoid Main(string[] args)

       {

           Start().Wait();

           Console.ReadLine();

       }

2. Move exact js to js file works.

C# Program.cs:

publicstaticasyncTask Start()

       {

           var func =Edge.Func(@"return require('../edgeEntryPoint.js')");

           Console.WriteLine(await func.Invoke(".NET"));

           //also can written like this

           //var invoke = func.Invoke(".NET");

           //var c = (dynamic)invoke.Result;

           //Console.WriteLine(c); //Node.js welcomes .NET

           //Console.WriteLine(await func(c)); //Node.js welcomesNode.js welcomes .NET

        }

       staticvoid Main(string[] args)

       {

           Start().Wait();

           Console.ReadLine();

       }

edgeEntryPoint.js:

module.exports = function (data, callback) {

   callback(null,'Node.js welcomes ' +data);

}

3. function has more than one parameters. works

C#:var invoke = func.Invoke(new {data1=".NET", data2=".NET2"});

JS:

这个method可以像正常node.js一样,require任何文件和调用任何function

module.exports =function (data, callback) {

   callback(null,'Node.js welcomes ' +data.data1 + data.data2);

}

0 0
原创粉丝点击