4

You can see the interop model for going from Node.js -> C#, here.

What I want to know is, can the C# code then make a call to a method in the Node.js part of the process from the C#, before returning?

Imagine if you had a call, like

var webApi = edge.func('/MyDotNetApi.csx');
webApi(function (error, result) { log.('api started'); });

where the MyDotNetApi.csx returns, but leaves a socket listener thread running to handle HTTP requests. Now, if the Node.js part of the process holds (ever changing) information which the .Net code needs to access for inclusion in its HTTP responses, can it somehow ask Node.js for it?

1 Answer 1

2

Calling back Node.js from C# with Edge.js is possible and documented.

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

3 Comments

Do you know if the resources passed into the closure are responsive to changes in the node process? E.g. could you pass in a function to the closure which accesses a node global variable and see updates to the global?
I'm not an expert in Node, but I think that should work. The important thing here is that the whole chain of calls (Node -> C# -> Node) must be executed on the same thread. Make sure you don't switch threads inside C# before you do the callback. Thus, you'd simply re-enter the Node context on a nested stack frame and you'd want to return from the callback as soon as possible, to avoid blocking the Node event loop.
Edge.js passes data between V8 and CLR by value, except for functions. If Node.js controls ever changing data that it wants to expose to .NET, it can do it in one of two ways: (1) by exposing a function from Node.js to .NET that allows .NET code to query for the current value as needed - example, or (2) by having .NET code return a function to Node.js which Node.js will invoke whenever the value of the data changes - example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.