Edge.js with newer versions of .NET ( 6 && 7 )

995 Views Asked by At

How can I use Edge.js with .NET version 6.0 ( or 7.0 ) and Node.js version v16.18.0?

After installing Edge.js using nuget ( dotnet add package EdgeJs --version 11.15.0 ) and importing it into my .csproj ( ) I wanna use it ( using EdgeJs; || using Edge ) but getting an error:

The type or namespace name 'EdgeJs' could not be found (are you missing a using directive or an assembly reference?).

1

There are 1 best solutions below

0
Rich N On

The original Edge.js by Tomasz Tjanczuk was last updated in 2017 and doesn't support the latest versions of .NET. It will still work in a .NET Framework app on Windows, and the inline code in the original documentation to call from C# into JavaScript still works in such an application. The latest version is 8.2.1.

There is a fork of the original project which is maintained and DOES support .NET 6.0. However, for .NET 6.0 and later you cannot call from C# into JavaScript using this project I think. The only direction of travel is to call from JavaScript to C# (and back to JavaScript). This doesn't need the NuGet package, just the npm package.

I got calls working from JavaScript to .NET 7.0 using the forked npm package and node.js 16.18.0 on Windows. The steps to do this are below:

  • Ensure node 16.18.0 is installed
  • Create a folder and create a C# .NET 7.0 class library project called 'QuickStart.NET7' in it.
  • Add a class called LocalMethods with the code below, which is taken from the quick start code for the forked project. You can delete any other existing class (Class1.cs):
    namespace QuickStart.NET7
    {
        public class LocalMethods
        {
            public async Task<object> UseDynamicInput(dynamic input)
            {
                return $".NET 7 welcomes {input}";
            }
        }
    }
  • Do a debug build of the .NET class library
  • Open a command prompt/terminal in your folder and do npm i [email protected]
  • Create a text file called main.js in the same folder, and paste the code below into it:
    const path = require('path');
    const baseDll = path.join(__dirname, 'QuickStart.NET7/QuickStart.NET7/bin/Debug/net7.0/QuickStart.NET7.dll');
    const localTypeName = 'QuickStart.NET7.LocalMethods';
    console.log('Base DLL:'+ baseDll);
    process.env.EDGE_USE_CORECLR = 1;
    const edge = require('edge-js');
    const useDynamicInput = edge.func({
        assemblyFile: baseDll,
        typeName: localTypeName,
        methodName: 'UseDynamicInput'
    });
    useDynamicInput('Node.Js', function(error, result) {
        if (error) throw error;
        console.log(result);
    });
  • Do node main.js in the command prompt/terminal

You should get output, coming from the C# function, of '.NET 7 welcomes Node.Js'. If you get a 'Could not load file or assembly' error check the Base DLL path is correct.

The same code as above works for .NET 6 as well. I have only tested this works on Windows, but the readme suggests it should work on Linux and MacOS for .NET 6.