花了一个多小时研究为什么C#调用jsfunction不work。这是梳理过程。
1 script Node.js from C# works.
public static async Task Start()
{
var func = Edge.Func(@"
returnfunction (data, callback) {
callback(null, 'Node.js welcomes ' + data);
}
");
Console.WriteLine(await func(".NET"));
}
static void Main(string[] args)
{
Start().Wait();
Console.ReadLine();
}
2. Move exact js to js file works.
C# Program.cs:
public static async Task 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
}
static void 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);
}