Edge: Calling node.js from .net

Created on 7 Feb 2017  Â·  5Comments  Â·  Source: tjanczuk/edge

I tried asking this question on slack but didn't get a response. Hence asking it here, apologies for that.

I need to notify the node.js application(electron in my case) when something happens in the .net code. How can I achieve that ?

Most helpful comment

I wouldn't try map it. I'd call function1 passing in the result from Response() as a parameter. Probably lots of ways to do this

public async void GetSomeValue()
{
    string response = Response();

    //pass the result to electron/node
   if(function1 != null)
  {
        await function1(response);
  }
}


All 5 comments

I have no relation to this repo. but I can tell you how I do it using electron-edge with an Electron app I have. C# code is loaded as a DLL, running on windows only.

  1. Bind the functions in the DLL in electron
let assemblyFile = __dirname + "/my.dll" //path to DLL, I'm simply storing the DLL in the current electron directory

const init = edge.func({
    assemblyFile : assemblyFile,
    typeName: 'MyNameSpace.MyClassName',
    methodName: 'InitCallbacks'   // I have a method in my MyClassName called this
});
  1. I create a javascript object with the functions I want to be called one way from the C# code
app.on('ready', () => {

...
const registerCallbacks = {
    function1 : (data) => {
           //doSomething with data
          console.log(data);
         },
    function2: (data) => {
        //doSometing with data
      console.log(data);
     }
    //and so on
    };
   //call the c# init  function passing in the object with the javascript functions
   init(registerCallBacks);
...
};
  1. In c# MyClass
public class MyClass {
 ...
 private Func<object, Task<object>> function1;
 private Func<object, Task<object>> function2;
 ...

 public async Task<object> InitCallbacks(dynamic input)
{
    return await Task.Run<object>(() =>
    {
                IDictionary<string, object> callbackFunctions = (IDictionary<string, object>)input;
               //assign the electron function to the class variable, 
                function1 = (Func<object, Task<object>>)callbackFunctions["function1"];
                function2 = (Func<object, Task<object>>)callbackFunctions["function2"];
                return new { };
       });

   }

}

I can then use function1, function2 in the c# code to call back to electron any time

if(function1 != null){
    await function1(someData);
}

Note sure if this is the same as electric-edge, but another JavaScript library intended to support .NET to NodeJS interfacing is EdgeJS.

Here is a nice MSDN article that describes it and how it relates to .NET and which may be of some help.
https://msdn.microsoft.com/en-us/magazine/mt703440.aspx https://msdn.microsoft.com/en-us/magazine/mt703440.aspx

Regards
Mike Wrobel

On Feb 7, 2017, at 3:20 AM, garysmi2 notifications@github.com wrote:

I have no relation to this repo. but I can tell you how I do it using electron-edge with an Electron app I have. C# code is loaded as a DLL, running on windows only.

Bind the functions in the DLL in electron
let assemblyFile = __dirname + "/my.dll" //path to DLL, I'm simply storing the DLL in the current electron directory

const init = edge.func({
assemblyFile : assemblyFile,
typeName: 'MyNameSpace.MyClassName',
methodName: 'InitCallbacks' // I have a method in my MyClassName called this
});
I create a javascript object with the functions I want to be called one way from the C# code
app.on('ready', () => {

...
const registerCallbacks = {
function1 : (data) => {
//doSomething with data
console.log(data);
},
function2: (data) => {
//doSometing with data
console.log(data);
}
//and so on
};
//call the c# init function passing in the object with the javascript functions
init(registerCallBacks);
...
};
In c# MyClass
public class MyClass {
...
private Func> function1;
private Func> function2;
...

public async Task InitCallbacks(dynamic input)
{
return await Task.Run(() =>
{
IDictionary callbackFunctions = (IDictionary)input;
//assign the electron function to the class variable,
function1 = (Func>)callbackFunctions["function1"];
function2 = (Func>)callbackFunctions["function2"];
return new { };
});

}

}
I can then use function1, function2 in the c# code to call back to electron any time

if(function1 != null){
await function1(someData);
}
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub https://github.com/tjanczuk/edge/issues/522#issuecomment-277930615, or mute the thread https://github.com/notifications/unsubscribe-auth/AXuxFGBz33vX8Gxr6otisI29CXG0gmI-ks5raClegaJpZM4L45xy.

@garysmi2 I have a function which returns some response from the WCF service. How do I map with the delegate function that you have declared ?
For eg:

public static string Response()
   {
      returns response;
   }

I need to map this function to function1. How do I do it ?

I wouldn't try map it. I'd call function1 passing in the result from Response() as a parameter. Probably lots of ways to do this

public async void GetSomeValue()
{
    string response = Response();

    //pass the result to electron/node
   if(function1 != null)
  {
        await function1(response);
  }
}


It worked for me thank you :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

agracio picture agracio  Â·  6Comments

0xF6 picture 0xF6  Â·  4Comments

timonsku picture timonsku  Â·  4Comments

carlskii picture carlskii  Â·  13Comments

kneumei picture kneumei  Â·  10Comments