JSReference Class

A reference to a value kept in the Node.js process, such as an object that cannot be serialized as JSON.

public sealed class JSReference : IAsyncDisposable, IDisposable

Remarks

The value is kept until the reference is disposed or the Node.js process exits. A reference can only be used with the process that created it. With NodeJsHostOptions.WorkerThreads, the value is kept by the worker thread that created it, and the calls that use the reference run on this worker thread; the value is lost when the worker thread exits or is terminated.

A JSReference converts implicitly to a JsonNode, so it can be used as an argument, or nested in a JsonObject or a JsonArray argument. The function receives the referenced value.

Example

await using var node = await NodeJsHost.StartAsync();

// A Hash object cannot be serialized as JSON, so it stays in the Node.js process
await using var hash = await node.InvokeReferenceAsync("node:crypto", "createHash", ["sha256"]);
await hash.InvokeVoidAsync("update", ["hello"]);

// A function receives the referenced value
await node.EvaluateVoidAsync("args[0].update(' world');", [hash]);

var digest = await hash.InvokeAsync("digest", ["hex"]);
Assert.Equal("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9", digest.GetString());
Implements

Properties

NameDescription
HostGets the host of the Node.js process that keeps the value.

Gets the host of the Node.js process that keeps the value.

public NodeJsHost Host { get; }

Property value

NodeJsHost

Methods

NameDescription
CreateInstanceAsync(string?, IReadOnlyList<JsonNode?>?, CancellationToken)Creates an instance (new) of the referenced class, or of a class that is a member of the referenced value, and keeps the instance in the Node.js process.
CreateInstanceAsync(string?, object?[]?, JsonSerializerOptions?, CancellationToken)Creates an instance (new) of the referenced class, or of a class that is a member of the referenced value, and keeps the instance in the Node.js process. Arguments are serialized using reflection.
Dispose()Releases the value, so Node.js can collect it. The release message is sent in the background.
DisposeAsync()Releases the value, so Node.js can collect it.
GetValueAsync(CancellationToken)Gets the JSON representation of the referenced value (awaited if it is a promise).
GetValueAsync<T>(JsonSerializerOptions?, CancellationToken)Gets the referenced value, deserialized from its JSON representation using reflection.
GetValueAsync<T>(JsonTypeInfo<T>, CancellationToken)Gets the referenced value, deserialized from its JSON representation.
GetValueAsync<T>(CancellationToken)Gets the referenced value, deserialized from its JSON representation using reflection.
InvokeAsync(string?, IReadOnlyList<JsonNode?>?, CancellationToken)Calls a method of the referenced value, or the referenced function.
InvokeAsync<T>(string?, IReadOnlyList<JsonNode?>?, JsonTypeInfo<T>, CancellationToken)Calls a method of the referenced value, or the referenced function, and deserializes the result.
InvokeAsync<T>(string?, object?[]?, JsonSerializerOptions?, CancellationToken)Calls a method of the referenced value, or the referenced function, and deserializes the result. Arguments are serialized using reflection.
InvokeAsync<T>(string?, CancellationToken)Calls a method of the referenced value without arguments, or the referenced function, and deserializes the result using reflection.
InvokeReferenceAsync(string?, IReadOnlyList<JsonNode?>?, CancellationToken)Calls a method of the referenced value, or the referenced function, and returns a reference to its result.
InvokeReferenceAsync(string?, object?[]?, JsonSerializerOptions?, CancellationToken)Calls a method of the referenced value, or the referenced function, and returns a reference to its result. Arguments are serialized using reflection.
InvokeVoidAsync(string?, IReadOnlyList<JsonNode?>?, CancellationToken)Calls a method of the referenced value, or the referenced function, and ignores its result.
InvokeVoidAsync(string?, object?[]?, JsonSerializerOptions?, CancellationToken)Calls a method of the referenced value, or the referenced function, and ignores its result. Arguments are serialized using reflection.
ToJsonNode()Converts the reference to a JsonNode, to use it as an argument or in a JsonObject or JsonArray argument.
ToString()Returns a string that represents the current object.

CreateInstanceAsync overloads

CreateInstanceAsync(string?, IReadOnlyList<JsonNode?>?, CancellationToken)

JSReference.cs

Creates an instance (new) of the referenced class, or of a class that is a member of the referenced value, and keeps the instance in the Node.js process.

public Task<JSReference> CreateInstanceAsync(string? memberName, IReadOnlyList<JsonNode?>? arguments = null, CancellationToken cancellationToken = default)
Parameters
memberName string?
The name of the member of the referenced value that is a class or a constructor function. When null, the referenced value is the class.
arguments IReadOnlyList<JsonNode?>?
The arguments passed to the constructor.
cancellationToken CancellationToken
A token to stop waiting for the result. The JavaScript code keeps running.
Returns

Task<JSReference> A reference to the new instance. Dispose it when the instance is no longer needed.

Exceptions
NodeJsException
The value is not a constructor, the constructor throws, or the Node.js process exits.

CreateInstanceAsync(string?, object?[]?, JsonSerializerOptions?, CancellationToken)

JSReference.cs

Creates an instance (new) of the referenced class, or of a class that is a member of the referenced value, and keeps the instance in the Node.js process. Arguments are serialized using reflection.

[RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use the overload that takes a JsonTypeInfo instead.")]
[RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo instead.")]
public Task<JSReference> CreateInstanceAsync(string? memberName, object?[]? arguments, JsonSerializerOptions? options, CancellationToken cancellationToken = default)
Parameters
memberName string?
The name of the member of the referenced value that is a class or a constructor function. When null, the referenced value is the class.
arguments object?[]?
The arguments passed to the constructor.
options JsonSerializerOptions?
A token to stop waiting for the result. The JavaScript code keeps running.
cancellationToken CancellationToken
Returns

Task<JSReference> A reference to the new instance. Dispose it when the instance is no longer needed.

Exceptions
NodeJsException
The value is not a constructor, the constructor throws, or the Node.js process exits.

Releases the value, so Node.js can collect it. The release message is sent in the background.

public void Dispose()

DisposeAsync()

JSReference.cs

Releases the value, so Node.js can collect it.

net10.0

public ValueTask DisposeAsync()

net11.0

public ValueTask DisposeAsync()

Returns

ValueTask

GetValueAsync overloads

GetValueAsync(CancellationToken)

JSReference.cs

Gets the JSON representation of the referenced value (awaited if it is a promise).

public Task<JsonElement> GetValueAsync(CancellationToken cancellationToken = default)
Parameters
cancellationToken CancellationToken
A token to stop waiting for the result.
Returns

Task<JsonElement>

Exceptions
NodeJsException
The value cannot be serialized, or the Node.js process exits.

GetValueAsync<T>(JsonSerializerOptions?, CancellationToken)

JSReference.cs

Gets the referenced value, deserialized from its JSON representation using reflection.

net10.0

[RequiresDynamicCode("JSON deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use the overload that takes a JsonTypeInfo instead.")]
[RequiresUnreferencedCode("JSON deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo instead.")]
public Task<T?> GetValueAsync<T>(JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)

net11.0

[RequiresDynamicCode("JSON deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use the overload that takes a JsonTypeInfo instead.")]
[RequiresUnreferencedCode("JSON deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo instead.")]
public Task<T?> GetValueAsync<T>(JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)
Type parameters
T
Parameters
options JsonSerializerOptions?
A token to stop waiting for the result.
cancellationToken CancellationToken
Returns

Task<T?>

Exceptions
JsonException
The result cannot be deserialized to T.

GetValueAsync<T>(JsonTypeInfo<T>, CancellationToken)

JSReference.cs

Gets the referenced value, deserialized from its JSON representation.

net10.0

public Task<T?> GetValueAsync<T>(JsonTypeInfo<T> resultTypeInfo, CancellationToken cancellationToken = default)

net11.0

public Task<T?> GetValueAsync<T>(JsonTypeInfo<T> resultTypeInfo, CancellationToken cancellationToken = default)
Type parameters
T
Parameters
resultTypeInfo JsonTypeInfo<T>
A token to stop waiting for the result.
cancellationToken CancellationToken
Returns

Task<T?>

Exceptions
JsonException
The result cannot be deserialized to T.

GetValueAsync<T>(CancellationToken)

JSReference.cs

Gets the referenced value, deserialized from its JSON representation using reflection.

[RequiresDynamicCode("JSON deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use the overload that takes a JsonTypeInfo instead.")]
[RequiresUnreferencedCode("JSON deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo instead.")]
public Task<T?> GetValueAsync<T>(CancellationToken cancellationToken)
Type parameters
T
Parameters
cancellationToken CancellationToken
A token to stop waiting for the result.
Returns

Task<T?>

Exceptions
JsonException
The result cannot be deserialized to T.

InvokeAsync overloads

InvokeAsync(string?, IReadOnlyList<JsonNode?>?, CancellationToken)

JSReference.cs

Calls a method of the referenced value, or the referenced function.

public Task<JsonElement> InvokeAsync(string? methodName, IReadOnlyList<JsonNode?>? arguments = null, CancellationToken cancellationToken = default)
Parameters
methodName string?
The name of the method, called with the referenced value as this. When the member is not a function, its value is returned. When null, the referenced value is called, so it must be a function.
arguments IReadOnlyList<JsonNode?>?
The arguments passed to the function.
cancellationToken CancellationToken
A token to stop waiting for the result. The JavaScript code keeps running.
Returns

Task<JsonElement> The JSON representation of the value returned by the function (awaited if it is a promise).

Exceptions
NodeJsException
The JavaScript code throws, or the Node.js process exits.

InvokeAsync<T>(string?, IReadOnlyList<JsonNode?>?, JsonTypeInfo<T>, CancellationToken)

JSReference.cs

Calls a method of the referenced value, or the referenced function, and deserializes the result.

net10.0

public Task<T?> InvokeAsync<T>(string? methodName, IReadOnlyList<JsonNode?>? arguments, JsonTypeInfo<T> resultTypeInfo, CancellationToken cancellationToken = default)

net11.0

public Task<T?> InvokeAsync<T>(string? methodName, IReadOnlyList<JsonNode?>? arguments, JsonTypeInfo<T> resultTypeInfo, CancellationToken cancellationToken = default)
Type parameters
T
Parameters
methodName string?
The name of the method, called with the referenced value as this. When the member is not a function, its value is returned. When null, the referenced value is called, so it must be a function.
arguments IReadOnlyList<JsonNode?>?
The arguments passed to the function.
resultTypeInfo JsonTypeInfo<T>
A token to stop waiting for the result. The JavaScript code keeps running.
cancellationToken CancellationToken
Returns

Task<T?> The JSON representation of the value returned by the function (awaited if it is a promise).

Exceptions
JsonException
The result cannot be deserialized to T.

InvokeAsync<T>(string?, object?[]?, JsonSerializerOptions?, CancellationToken)

JSReference.cs

Calls a method of the referenced value, or the referenced function, and deserializes the result. Arguments are serialized using reflection.

net10.0

[RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use the overload that takes a JsonTypeInfo instead.")]
[RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo instead.")]
public Task<T?> InvokeAsync<T>(string? methodName, object?[]? arguments = null, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)

net11.0

[RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use the overload that takes a JsonTypeInfo instead.")]
[RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo instead.")]
public Task<T?> InvokeAsync<T>(string? methodName, object?[]? arguments = null, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)
Type parameters
T
Parameters
methodName string?
The name of the method, called with the referenced value as this. When the member is not a function, its value is returned. When null, the referenced value is called, so it must be a function.
arguments object?[]?
The arguments passed to the function.
options JsonSerializerOptions?
A token to stop waiting for the result. The JavaScript code keeps running.
cancellationToken CancellationToken
Returns

Task<T?> The JSON representation of the value returned by the function (awaited if it is a promise).

Exceptions
JsonException
The result cannot be deserialized to T.

InvokeAsync<T>(string?, CancellationToken)

JSReference.cs

Calls a method of the referenced value without arguments, or the referenced function, and deserializes the result using reflection.

[RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use the overload that takes a JsonTypeInfo instead.")]
[RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo instead.")]
public Task<T?> InvokeAsync<T>(string? methodName, CancellationToken cancellationToken)
Type parameters
T
Parameters
methodName string?
The name of the method, called with the referenced value as this. When the member is not a function, its value is returned. When null, the referenced value is called, so it must be a function.
cancellationToken CancellationToken
The arguments passed to the function.
Returns

Task<T?> The JSON representation of the value returned by the function (awaited if it is a promise).

Exceptions
JsonException
The result cannot be deserialized to T.

InvokeReferenceAsync overloads

InvokeReferenceAsync(string?, IReadOnlyList<JsonNode?>?, CancellationToken)

JSReference.cs

Calls a method of the referenced value, or the referenced function, and returns a reference to its result.

public Task<JSReference> InvokeReferenceAsync(string? methodName, IReadOnlyList<JsonNode?>? arguments = null, CancellationToken cancellationToken = default)
Parameters
methodName string?
The name of the method, called with the referenced value as this. When the member is not a function, its value is returned. When null, the referenced value is called, so it must be a function.
arguments IReadOnlyList<JsonNode?>?
The arguments passed to the function.
cancellationToken CancellationToken
A token to stop waiting for the result. The JavaScript code keeps running.
Returns

Task<JSReference> A reference to the value returned by the function (awaited if it is a promise). Dispose it when the value is no longer needed.

Exceptions
NodeJsException
The JavaScript code throws, or the Node.js process exits.

InvokeReferenceAsync(string?, object?[]?, JsonSerializerOptions?, CancellationToken)

JSReference.cs

Calls a method of the referenced value, or the referenced function, and returns a reference to its result. Arguments are serialized using reflection.

[RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use the overload that takes a JsonTypeInfo instead.")]
[RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo instead.")]
public Task<JSReference> InvokeReferenceAsync(string? methodName, object?[]? arguments, JsonSerializerOptions? options, CancellationToken cancellationToken = default)
Parameters
methodName string?
The name of the method, called with the referenced value as this. When the member is not a function, its value is returned. When null, the referenced value is called, so it must be a function.
arguments object?[]?
The arguments passed to the function.
options JsonSerializerOptions?
A token to stop waiting for the result. The JavaScript code keeps running.
cancellationToken CancellationToken
Returns

Task<JSReference> A reference to the value returned by the function (awaited if it is a promise). Dispose it when the value is no longer needed.

Exceptions
NodeJsException
The JavaScript code throws, or the Node.js process exits.

InvokeVoidAsync overloads

InvokeVoidAsync(string?, IReadOnlyList<JsonNode?>?, CancellationToken)

JSReference.cs

Calls a method of the referenced value, or the referenced function, and ignores its result.

public Task InvokeVoidAsync(string? methodName, IReadOnlyList<JsonNode?>? arguments = null, CancellationToken cancellationToken = default)
Parameters
methodName string?
The name of the method, called with the referenced value as this. When the member is not a function, its value is returned. When null, the referenced value is called, so it must be a function.
arguments IReadOnlyList<JsonNode?>?
The arguments passed to the function.
cancellationToken CancellationToken
A token to stop waiting for the result. The JavaScript code keeps running.
Returns

Task The JSON representation of the value returned by the function (awaited if it is a promise).

Exceptions
NodeJsException
The JavaScript code throws, or the Node.js process exits.

InvokeVoidAsync(string?, object?[]?, JsonSerializerOptions?, CancellationToken)

JSReference.cs

Calls a method of the referenced value, or the referenced function, and ignores its result. Arguments are serialized using reflection.

[RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use the overload that takes a JsonTypeInfo instead.")]
[RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo instead.")]
public Task InvokeVoidAsync(string? methodName, object?[]? arguments, JsonSerializerOptions? options, CancellationToken cancellationToken = default)
Parameters
methodName string?
The name of the method, called with the referenced value as this. When the member is not a function, its value is returned. When null, the referenced value is called, so it must be a function.
arguments object?[]?
The arguments passed to the function.
options JsonSerializerOptions?
A token to stop waiting for the result. The JavaScript code keeps running.
cancellationToken CancellationToken
Returns

Task The JSON representation of the value returned by the function (awaited if it is a promise).

Exceptions
NodeJsException
The JavaScript code throws, or the Node.js process exits.

ToJsonNode()

JSReference.cs

Converts the reference to a JsonNode, to use it as an argument or in a JsonObject or JsonArray argument.

public JsonNode ToJsonNode()

Returns

JsonNode

ToString()

JSReference.cs

Returns a string that represents the current object.

public override string ToString()

Returns

string A string that represents the current object.

Operators

NameDescription
implicit operator JsonNode?(JSReference?)Converts the reference to a JsonNode, to use it as an argument or in a JsonObject or JsonArray argument.

implicit operator JsonNode?(JSReference?)

JSReference.cs

Converts the reference to a JsonNode, to use it as an argument or in a JsonObject or JsonArray argument.

public static implicit operator JsonNode?(JSReference? value)

Parameters

value JSReference?

Returns

JsonNode?