How to accomplish two way communication between .NET and JS?
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 35/100
- issue の種類
- バグ
- 明瞭さ
- 説明が足りない
- 活発さ
- 停滞
- 技術スタック
- csharp, javascript
- 領域
- backend
調査の方向性
https://microsoft.github.io/node-api-dotnet/ のドキュメント、issue #330、および node-api-dotnet v0.8.8 の package exports から始めてください。C# と ES module の例を使って napi_create_string_utf16 と package-subpath のエラーを再現してください。v0.8.8 でドキュメントに記載された import と双方向通信のフローが機能するかどうかを判断し、修正されたガイダンスまたは確認済みのライブラリの問題を提示できれば完了です。
索引モデルが issue の本文から書いたものです。
説明
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?
- 主要言語
- C#
- スター
- 783
- フォーク
- 80
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
microsoft/node-api-dotnet のほかの issue
-
難易度 1/5 1〜3時間 初心者へのやさしさ 88/100
microsoft/node-api-dotnet#484 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
microsoft/node-api-dotnet#481 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 82/100
microsoft/node-api-dotnet#502 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
microsoft/node-api-dotnet#486 · コメント 1 件 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
microsoft/node-api-dotnet#483 · コメント 1 件 · リアクション 1 件 ·
microsoft/node-api-dotnet の issue をすべて見る
似ている issue
-
needs triage
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
spectreconsole/spectre.console#2221 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
godotengine/godot-docs#12428 ·
メンテナーはふだん 1 日以内に返信
-
.NET triage
難易度 2/5 1〜3時間 初心者へのやさしさ 73/100
microsoft/semantic-kernel#14526 ·
メンテナーはふだん 4 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 92/100