Importing `node-api-dotnet` into node into a dotnet app
@jasongin arbeitet bereits daran.
Seit 24.7.2024.
Bewertung
Dieses Issue wurde noch nicht bewertet.
Beschreibung
The use case here is to create a runtime "script editor" that uses javascript/typescript as the language.
So the goal is:
- Running in dotnet host program
- User writes javascript that can interop with net objects
- Javascript is executed by the dotnet side.
I'm having issues calling the javascript import of node-api-dotnet when it's running inside the host process. Here's a reproducible example, where the TEST_MS flag just makes sure it does work with the simple ms package.
//#define TEST_MS
// See https://aka.ms/new-console-template for more information
using Microsoft.JavaScript.NodeApi;
using Microsoft.JavaScript.NodeApi.Runtime;
using System.Reflection;
Console.WriteLine("Hello, World!");
string baseDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!;
File.WriteAllText(Path.Combine(baseDir, "package.json"), $"{{\"type\":\"module\"}}");
NodejsPlatform platform = new NodejsPlatform(Path.Combine(baseDir, "native", "win-x64", "libnode.dll"));
NodejsEnvironment env = platform.CreateEnvironment(baseDir);
await env.RunAsync(async () =>
{
#if TEST_MS
JSValue console = JSValue.CreateObject();
console.SetProperty("info", JSValue.CreateFunction("info", (args) =>
{
for (int i = 0; i < args.Length; i++)
{
Console.WriteLine(args[i].GetValueExternalOrPrimitive()!.ToString());
}
return JSValue.Undefined;
}));
JSValue.Global.SetProperty("console", console);
JSValue.RunScript("const ms = require(\"C:/node_modules/ms\"); " +
"console.info(\"data\", ms(1231234));");
#else
JSValue.RunScript("const dotnet = require(\"C:/node_modules/node-api-dotnet\");");
Console.WriteLine("Finished.");
#endif
});
The exception I get here is:
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
Which is the same when using await env.ImportAsync("C:/node_modules/node-api-dotnet/net8.0", null, true);
It did occur to me that the Microsoft.JavaScript.NodeApi.dll exists in both the dotnet project and the node module here. Any ideas on how to resolve this?
- Vorherrschende Sprache
- C#
- Sterne
- 783
- Forks
- 80
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Entwicklungsumgebung
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus microsoft/node-api-dotnet
-
Schwierigkeit 1/5 1-3 Stunden Anfängerfreundlichkeit 88/100
microsoft/node-api-dotnet#484 ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
microsoft/node-api-dotnet#481 ·
-
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 35/100
microsoft/node-api-dotnet#486 · 1 Kommentar ·
-
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 35/100
microsoft/node-api-dotnet#483 · 1 Kommentar · 1 Reaktion ·
-
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 45/100
microsoft/node-api-dotnet#479 ·
Alle Issues in microsoft/node-api-dotnet
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
SubtitleEdit/subtitleedit#15462 ·
Maintainer antworten meist innerhalb von 1 Tag
-
:watch: Not Triaged
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
Maintainer antworten meist innerhalb von 1 Tag
-
comp:instrumentation.aspnetcore
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
open-telemetry/opentelemetry-dotnet-contrib#5427 ·
Maintainer antworten meist innerhalb von 1 Tag
-
design-proposal
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
dotnet/aspnetcore#69592 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Client Container Registry customer-reported needs-team-attention question Service Attention
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
Azure/azure-sdk-for-net#63470 · 3 Kommentare · 1 Reaktion ·
Maintainer antworten meist innerhalb von 1 Tag