Skip to content

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

@yannickcholette

Description

@yannickcholette

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions