0

I need to do some calls from my js to my C# DLL in my Electron project and it works fine in this way:

c#

namespace Electron.Wrapper
{
    public class QueryWrapper
    {

        public async Task<object> OnQuery(dynamic request)
        {
            ...
            return ..;
        }
    }
}

js

let edge = require('electron-edge-js');
let queryWrapperQuery = edge.func({
    assemblyFile: '..dllUrl..',
    typeName: 'Electron.Wrapper.QueryWrapper',
    methodName: 'OnQuery'
});
window.query = function (options) {
    queryWrapperQuery(JSON.stringify(options), function (error, result) {
        ...
    });
}

The problem is that I use an external DLL that triggers async events sometimes, so I need find a way for listening the .NET events from js.

I found this way for resolve my problem but I think isn't the right way because I need a class library for Electron and I don't know how use it with also the previous way and probabily I don't need a WebSocketServer.

A .Net and js sample will be valued.

Thanks, Andrea

Update 1 I found this way, cold be the right one? I'm trying to implement .net, any suggestions?

1
  • it depends on how deep you want to link between nodejs and c# code. I can think of simple solution like writing to a file and the code on both sides can listen to file changes. Or you can use redis as a middle bridge Commented Apr 19, 2019 at 7:25

1 Answer 1

2

I found a good way:

C#:

public Task<object> WithCallback(IDictionary<string, object> payload)
{
    Func<object, Task<object>> changed = (Func<object, Task<object>>)payload["changed"];
    return Task.Run(async () => await OnQuery(payload["request"], changed));
}

js:

var withCallback = edge.func({
    assemblyFile: '..dllUrl..',
    typeName: 'Electron.Wrapper.QueryWrapper',
    methodName: 'WithCallback' 
});

window.query = function (options) {
    function triggerResponse(error, result) {
        ...
    }

    withCallback({
        changed: (result) => triggerResponse(null, result),
        request: JSON.stringify(options)
    }, triggerResponse);
};

When you need trigger when someting changes you should use the parameter 'payload' in OnQuery function:

public async Task<object> OnQuery(dynamic request, dynamic payload = null)
{
    ...
}

Next the OnQuery return the value you can call again the js callback in this way:

payload("Notify js callback!");

I hope this can help someone!

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

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.