microsoft / microsoft/node-api-dotnet

JSValue returned by Import() don't survive dependency injection

Open
#444 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
783
Forks
80
PR merge metrics
No merged PRs in 30d

Description

I'm not sure if this is supposed to work, but my use case is that I want to load a module once, then pass it around to various constructors using dependency injection so that they can call methods.

So I do

await _nodeJS.SynchronizationContext.RunAsync(async () =>
{
   JSReference cspellModule = new(await _nodeJS.ImportAsync("./loadcspell", esModule: true)); 
   builder.RegisterInstance(cspellModule);  
}

And then later on I receive this reference in a constructor and try to call a method:

await _nodeJS.SynchronizationContext.RunAsync(async () =>
{
   var spellCheckFileMethod = (JSPromise)cspellModule.GetValue().CallMethod("spellCheckFile", ...);
   var spellCheckFileResults = await spellCheckFileMethod.AsTask(context.CancellationToken);
}

And got this:

Microsoft.JavaScript.NodeApi.JSException
  HResult=0x80131500
  Message=Invalid argument: Error in Call at D:\a\_work\1\s\src\NodeApi\JSValue.cs:666
  StackTrace:
<Cannot evaluate the exception stack trace>

It works when loading the module locally.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at src/NodeApi/JSValue.cs:666 and reproduce the failure using the ImportAsync, RegisterInstance, and constructor-injection flow shown in the issue. Compare it with the locally loaded module that works, focusing on how the JSReference survives between synchronization-context calls. Done means an imported module can be passed through dependency injection and its spellCheckFile method can be called successfully later.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, javascript, node.js
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.