Hacktoberfest 2026: die Issues, die Maintainer für den Oktober markiert haben – offen und einsteigerfreundlich. Hacktoberfest-Issues durchsuchen

How to accomplish two way communication between .NET and JS?

Offen
#381 2 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
35/100
Issue-Typ
Bug
Klarheit
Muss geklärt werden
Aktivitätsstatus
Veraltet
Tech-Stack
csharp, javascript
Bereich
backend

Rechercherichtung

Beginne mit der Dokumentation unter https://microsoft.github.io/node-api-dotnet/, issue #330, und den Paketexporten von node-api-dotnet v0.8.8. Reproduziere die Fehler napi_create_string_utf16 und package-subpath mithilfe der C#- und ES-Modul-Beispiele; abgeschlossen ist die Aufgabe, wenn festgestellt wurde, ob der dokumentierte Import- und bidirektionale Kommunikationsablauf für v0.8.8 funktioniert, mit korrigierter Anleitung oder bestätigtem Bibliotheksproblem.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

I'm struggling to implement two way communication with this library. Issue might be related to #330.

I'm using .NET as my main program from which I load my JS (ES module, not CommonJS) like this:

[JSExport]
public class TestClass
{
    private NodejsEnvironment NodeJSEnvironment;
    private NodejsPlatform nodejsPlatform;

    public async Task Start()
    {
        var baseDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!;
        var libnodePath = "/path/to/libnode.dll";
        nodejsPlatform = new(libnodePath);
        NodeJSEnvironment = nodejsPlatform.CreateEnvironment("/path/to/project");
        var pid = Process.GetCurrentProcess().Id;
        var inspectionUri = NodeJSEnvironment.StartInspector();
        await NodeJSEnvironment.SynchronizationContext.RunAsync(async () =>
        {
            try
            {
                var jsModule = await NodeJSEnvironment.ImportAsync("./dist/TSTest.js", esModule: true);
                var x = (int)jsModule.CallMethod("callFromDotNet");
            }
            catch (Exception e)
            {
                throw;
            }
        });
    }

    [JSExport]
    public static void CallFromJS()
    {
        Console.WriteLine("Call From JS Success");
    }
}

And my JS file looks like this:

import dotnet from 'node-api-dotnet';

const testClass = dotnet.require('TestClass');
testClass.StaticClass.CallFromJS();

function callFromDotNet() {
    return 42;
}
export { callFromDotNet };

I am using version 0.8.8 for micorosft.javascript.nodeapi and nodeapi.generator nugets. I'm using the same version for JS import.

Issue is that when importing package to JS file I get a crashing error:

Unhandled Exception: System.EntryPointNotFoundException: Arg_EntryPointNotFoundExceptionParameterizedNoLibrary, napi_create_string_utf16
   at System.Runtime.InteropServices.NativeLibrary.GetSymbol(IntPtr, String, Boolean) + 0x57
   at Microsoft.JavaScript.NodeApi.Runtime.NodejsRuntime.CreateString(JSRuntime.napi_env, ReadOnlySpan`1, JSRuntime.napi_value&) + 0x62
   at Microsoft.JavaScript.NodeApi.JSValue.CreateStringUtf16(String) + 0x72
   at Microsoft.JavaScript.NodeApi.JSValue.op_Implicit(String) + 0x15
   at Microsoft.JavaScript.NodeApi.DotNetHost.NativeHost.InitializeModule(JSRuntime.napi_env, JSRuntime.napi_value) + 0x435

I realize that in documentation it says to use import statement like import dotnet from 'node-api-dotnet/net8.0.js'; with ES module but this is also not working for me. I get error: JSException: Package subpath './net8.0.js' is not defined by "exports"

Is documentation found at https://microsoft.github.io/node-api-dotnet/ outdated?

With this code if I remove dotnet import statement and just keep the exported callFromDotNet function from .js file I am able to call JS functions from .NET (one way communication), but how can I also call .NET methods from this same .js file?

Is two way communication like this possible? What am I doing wrong?

Vorherrschende Sprache
C#
Sterne
783
Forks
80
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus microsoft/node-api-dotnet

Alle Issues in microsoft/node-api-dotnet

Ähnliche Issues

Weitere Issues zu C#

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.