0

I'm trying to use edge.js for using a node.js module within C#.

I can access a property of the module, but not a method.

using System;
using System.Threading.Tasks;
using EdgeJs;

class Program
{

    static void Main(string[] args)
    {
        Start().Wait();
    }

    public static async Task Start()
    {
        var func = Edge.Func(@"return require('../require.js')");
        var invoke = func.Invoke(new { });
        var require = (dynamic)invoke.Result;

        Console.WriteLine(require.answer1); // answer 1

        var deleg = require.answer2;
        Console.WriteLine(deleg);
        var task = deleg.Invoke(1); 
        Console.WriteLine(task);

        // var result = await task.Run(); // error
        // Console.WriteLine(result);


    }

}

require.js

module.exports = function (data, callback) {
    var module = require('./module');
    var result = module;
    callback(null, result);
};

module.js

exports.answer1 = "answer 1";
exports.answer2 = function () {
    return "answer 2";
};

Is there a way I can call the function in node.js from C# and access "answer 2"?

Here the error:

answer 1
System.Func`2[System.Object,System.Threading.Tasks.Task`1[System.Object]]
System.Threading.Tasks.Task`1[System.Object]

Unhandled Exception: System.AggregateException: One or more errors occurred. ---
> Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: No overload for method
'Run' takes '0' arguments
   at CallSite.Target(Closure , CallSite , Object )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T
0 arg0)
   at Program.<Start>d__d.MoveNext() in c:\Users\admin\Documents\Visual Studio 2
013\Projects\snippets\snippets\Program.cs:line 26
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceled
Exceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationTo
ken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at Program.Main(String[] args) in c:\Users\admin\Documents\Visual Studio 2013
\Projects\snippets\snippets\Program.cs:line 10
Press any key to continue . . .
7
  • What does Console.WriteLine(deleg) produce? Commented Mar 8, 2016 at 17:21
  • System.Func2[System.Object,System.Threading.Tasks.Task1[System.Object]] Commented Mar 8, 2016 at 17:23
  • "No overload for method 'Run' takes '0' arguments" means I think there are missing arguments but the node.js method does not need arguments, so I don't understand what arguments should be send. Commented Mar 8, 2016 at 17:27
  • So I'm not an edge.js expert (just trying to help), but it seems like you should be doing something more like: var result = await task. No need to call the run method on the task, the await keyword should invoke the task if it hasn't already been completed. Commented Mar 8, 2016 at 17:29
  • Thanks, no error anymore, but no output either, the console seems to hang. Commented Mar 8, 2016 at 17:39

2 Answers 2

2

I think you need to adjust your code to the following:

var deleg = require.answer2;
Console.WriteLine(deleg);
var task = (Func<object,Task<object>>)deleg; 
Console.WriteLine(task);

var result = await task(1);
Console.WriteLine(result);

I took this directly from the github readme, but essentially calling invoke on the function doesn't actually work like it would with a normal synchronous delegate, instead it just returns control immediately but does not provide a scheduler that can actually run the task.

Sign up to request clarification or add additional context in comments.

3 Comments

I can't get anything for result. It outputs deleg: System.Func2[System.Object,System.Threading.Tasks.Task1[System.Object]] and for task: System.Func2[System.Object,System.Threading.Tasks.Task1[System.Object]]
What does the last line output, or does it still hang at the await?
this was part of the solution anyway!
0

The answer would be to change module.js to:

exports.answer2 = function (input, cb) {
  cb(null, "answer 2");
};

I got the answer from edge.js

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.