Search code examples
c#node.jsedge.js

Using Edge.js from a C# console application


I'm trying to do something that seems very simple, yet I cannot get it to work. I've installed Edge.js via nuget and I want to execute a piece of Javascript from inside a C# console application. The code from my public static void main is below:

Task.Run(async () => {
    Console.WriteLine(
        await Edge.Func("return function() { return 'hello world'; }")(null).Result
    );
}).Wait();

I run the application and all it does is hangs.

I've tried this:

Console.WriteLine(Edge.Func("return function() { return 'hello'; }")(null).Result);

Same result. And this too...

var task = Edge.Func("return function() { return 'hello'; }")(null);
await task;
Console.WriteLine(task.Result);

Same result. Hanging...

I also tried RunSynchronously() rather than awaiting, which gave me an InvalidOperationException.

I followed the documentation here.

Any ideas?


Solution

  • You have to use the callback to send the string. You can't just return the string. See:

    Edge.Func(@"
            return function (data, callback) {
                callback(null, 'hello world');
            }
        ");
    

    You have to follow this structure when using Edge:

    Edge