user2560413
user2560413

Reputation: 77

How to call functions in a C# dll file from a node.js file

**I created a C# dll file that contains functions that returns some values, and I am trying to call these functions from a node.js file to get those values. Please how do I go about this?

Upvotes: 6

Views: 16174

Answers (2)

Huan
Huan

Reputation: 3247

I believe the Edge.js - Run .NET and Node.js code in-process on Windows, macOS, and Linux is what you need.

By using Edge.js, we can write the following JavaScript codes that invoke existing C# programs.

var clrMethod = edge.func({
    assemblyFile: 'My.Edge.Samples.dll',
    typeName: 'Samples.FooBar.MyType',
    methodName: 'MyMethod' // This must be Func<object,Task<object>>
});

Or

var clrMethod = edge.func('My.Edge.Samples.dll');

Or

var add7 = edge.func(require('path').join(__dirname, 'add7.csx'));

The above three sample codes are all copy/pasted from their GitHub home page.

Upvotes: 3

jfriend00
jfriend00

Reputation: 707328

The documentation for how to implement a native code nodejs addon is here: Node.js v0.12.7 Addons and there are numerous examples included in that documentation.

More specific discussion of doing an addon in C# here: Using a .NET DLL in Node.js / serverside javascript and .net native extension for node.js

I can imagine it gets a bit tricky trying to hook up two garbage collected languages since automatic garbage collection probably isn't completely possible both ways.

Upvotes: 2

Related Questions