2

I wonder if it is possible to call a C# function from javascript like I try to do below as an example? (I use Node.js)

Is it possible to combine this in anyway to let the csharpfunction use its way to calculate doubles, Int32 etc csharp types for calculation?

var a = 4;
var b = 3;
var array = ["1", "2"];

//Is it possible to call the "csharpfunction" from javascript?


double csharpfunction(double a, double b, string[] array)
{
    a = a * b;
    return a;
}

1 Answer 1

2

Because C# has its own garbage collection system and thus needs its own infrastructure running and its data formats are completely different and custom, it would probably be simplest to just wrap your function in a super simple server, run that separate process from your node.js app and then send requests to that server (probably via localhost on a predetermined port) to execute whatever you're trying to do. You can decide what type of server and data interchange format you want to use, but HTTP and JSON would be simple and quick to implement as long as they didn't cause efficiency issues. For the data to go between languages, it's going to have to be put in some standard format any way.

node.js has an addon system, but it is geared to C/C++ so you'd probably have to wrap your C# code in a C/C++ wrapper to use it directly and you'd still probably have to put your C# code in its own process in order to allow its garbage collector to have a place to run and do its thing. My sense is that a simple server in its own process is probably even easier than using the addon system in this case.

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

1 Comment

I see, so I should use an own process in someway and pass the calculations there. It sounds like a good and clean way to do it. Thank you!

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.