NodeJsHostPool Class
Runs JavaScript code on a fixed number of Node.js processes, so CPU-bound code can run in parallel.
public sealed class NodeJsHostPool : IAsyncDisposableRemarks
Each call is sent to the host with the fewest calls in progress, including canceled calls whose JavaScript code has not completed. When NodeJsHostOptions.MaxConcurrentCalls is set, calls wait in the pool until a host can run them, so a host busy with a long call does not delay the following calls. Hosts do not share state: values stored on globalThis are only visible to calls running on the same host. Use NodeJsHostPool.RunAsync to run several calls on the same host.
When a Node.js process exits, its host is replaced by a new one the next time a host is selected. Set NodeJsHostOptions.UnresponsiveTimeout to also replace a process whose event loop is blocked by a canceled call. With NodeJsHostOptions.WorkerThreads, each process runs the calls on several worker threads, and only the worker thread blocked by a canceled call is restarted.
Example
string[] documents = ["# Title", "Some *text*"];
await using var pool = await NodeJsHostPool.StartAsync(Environment.ProcessorCount);
// Each call runs on the host with the fewest calls in progress, so the calls run in parallel
var results = await Task.WhenAll(documents.Select(document => pool.EvaluateAsync("return args[0].length;", [document])));
Assert.Equal([7, 11], results.Select(result => result.GetInt32()));- Implements
Properties
| Name | Description |
|---|---|
| Size | Gets the number of Node.js processes in the pool. |
Methods
| Name | Description |
|---|---|
| CreateInstanceAsync(string, string?, IReadOnlyList<JsonNode?>?, CancellationToken) | Imports a module, creates an instance of one of its exported classes (new), and keeps the instance in the Node.js process. |
| CreateInstanceAsync(string, string?, object?[]?, JsonSerializerOptions?, CancellationToken) | Imports a module, creates an instance of one of its exported classes (new), and keeps the instance in the Node.js process. Arguments are serialized using reflection. |
| DisposeAsync() | Stops all the Node.js processes. Pending calls fail with ObjectDisposedException. |
| EvaluateAsync(string, IReadOnlyList<JsonNode?>?, CancellationToken) | Evaluates JavaScript code as the body of an async function that receives arguments. |
| EvaluateAsync(string, CancellationToken) | Evaluates JavaScript code as the body of an async function. |
| EvaluateAsync<T>(string, IReadOnlyList<JsonNode?>?, JsonTypeInfo<T>, CancellationToken) | Evaluates JavaScript code as the body of an async function that receives arguments, and deserializes the result. |
| EvaluateAsync<T>(string, object?[]?, JsonSerializerOptions?, CancellationToken) | Evaluates JavaScript code as the body of an async function that receives arguments, and deserializes the result. Arguments are serialized using reflection. |
| EvaluateAsync<T>(string, JsonSerializerOptions?, CancellationToken) | Evaluates JavaScript code as the body of an async function and deserializes the result using reflection. |
| EvaluateAsync<T>(string, JsonTypeInfo<T>, CancellationToken) | Evaluates JavaScript code as the body of an async function and deserializes the result. |
| EvaluateAsync<T>(string, CancellationToken) | Evaluates JavaScript code as the body of an async function and deserializes the result using reflection. |
| EvaluateReferenceAsync(string, IReadOnlyList<JsonNode?>?, CancellationToken) | Evaluates JavaScript code as the body of an async function that receives arguments, and keeps the result in the Node.js process. |
| EvaluateReferenceAsync(string, object?[]?, JsonSerializerOptions?, CancellationToken) | Evaluates JavaScript code as the body of an async function that receives arguments, and keeps the result in the Node.js process. Arguments are serialized using reflection. |
| EvaluateReferenceAsync(string, CancellationToken) | Evaluates JavaScript code as the body of an async function, and keeps the result in the Node.js process. |
| EvaluateVoidAsync(string, IReadOnlyList<JsonNode?>?, CancellationToken) | Evaluates JavaScript code as the body of an async function that receives arguments, ignoring its result. The result does not need to be serializable. |
| EvaluateVoidAsync(string, object?[]?, JsonSerializerOptions?, CancellationToken) | Evaluates JavaScript code as the body of an async function that receives arguments, ignoring its result. Arguments are serialized using reflection. |
| EvaluateVoidAsync(string, CancellationToken) | Evaluates JavaScript code as the body of an async function, ignoring its result. The result does not need to be serializable. |
| InvokeAsync(string, string?, IReadOnlyList<JsonNode?>?, CancellationToken) | Imports a module and calls one of its exports. |
| InvokeAsync<T>(string, string?, IReadOnlyList<JsonNode?>?, JsonTypeInfo<T>, CancellationToken) | Imports a module, calls one of its exports, and deserializes the result. |
| InvokeAsync<T>(string, string?, object?[]?, JsonSerializerOptions?, CancellationToken) | Imports a module, calls one of its exports, and deserializes the result. Arguments are serialized using reflection. |
| InvokeAsync<T>(string, string?, CancellationToken) | Imports a module, calls one of its exports without arguments, and deserializes the result using reflection. |
| InvokeReferenceAsync(string, string?, IReadOnlyList<JsonNode?>?, CancellationToken) | Imports a module, calls one of its exports, and keeps the result in the Node.js process. |
| InvokeReferenceAsync(string, string?, object?[]?, JsonSerializerOptions?, CancellationToken) | Imports a module, calls one of its exports, and keeps the result in the Node.js process. Arguments are serialized using reflection. |
| InvokeVoidAsync(string, string?, IReadOnlyList<JsonNode?>?, CancellationToken) | Imports a module and calls one of its exports, ignoring its result. The result does not need to be serializable. |
| InvokeVoidAsync(string, string?, object?[]?, JsonSerializerOptions?, CancellationToken) | Imports a module and calls one of its exports, ignoring its result. Arguments are serialized using reflection. |
| RunAsync(Func<NodeJsHost, Task>, CancellationToken) | Runs a function with one host of the pool. Use it to run several calls on the same Node.js process. |
| RunAsync<T>(Func<NodeJsHost, Task<T>>, CancellationToken) | Runs a function with one host of the pool. Use it to run several calls on the same Node.js process. |
| StartAsync(int, NodeJsHostOptions?, CancellationToken) | Starts a pool of Node.js processes. |
CreateInstanceAsync overloads
CreateInstanceAsync(string, string?, IReadOnlyList<JsonNode?>?, CancellationToken)
NodeJsHostPool.csImports a module, creates an instance of one of its exported classes (new), and keeps the instance in the Node.js process.
public Task<JSReference> CreateInstanceAsync(string module, string? exportName, IReadOnlyList<JsonNode?>? arguments = null, CancellationToken cancellationToken = default)Parameters
modulestring- The module specifier, as used by
import(): an npm package name, a relative path starting with./or../and including the file extension, resolved fromNodeJsHostOptions.WorkingDirectory, an absolute path, or afile:ornode:URL. A module is loaded once, and kept for the lifetime of the process. exportNamestring?- The name of the exported class or constructor function. When
null, the default export is used. argumentsIReadOnlyList<JsonNode?>?- The arguments passed to the constructor. Use
JSValuefor values that JSON cannot represent, andJSReferenceto pass a value kept in the Node.js process. cancellationTokenCancellationToken- 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 export is not a constructor, the constructor throws, or the Node.js process exits.
Remarks
The reference is bound to the host that ran the call. When the arguments contain a JSReference, the call runs on the host that keeps the referenced value.
CreateInstanceAsync(string, string?, object?[]?, JsonSerializerOptions?, CancellationToken)
NodeJsHostPool.csImports a module, creates an instance of one of its exported classes (new), 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 module, string? exportName, object?[]? arguments, JsonSerializerOptions? options, CancellationToken cancellationToken = default)Parameters
modulestring- The module specifier, as used by
import(): an npm package name, a relative path starting with./or../and including the file extension, resolved fromNodeJsHostOptions.WorkingDirectory, an absolute path, or afile:ornode:URL. A module is loaded once, and kept for the lifetime of the process. exportNamestring?- The name of the exported class or constructor function. When
null, the default export is used. argumentsobject?[]?- The arguments passed to the constructor. Use
JSValuefor values that JSON cannot represent, andJSReferenceto pass a value kept in the Node.js process. optionsJsonSerializerOptions?- A token to stop waiting for the result. The JavaScript code keeps running.
cancellationTokenCancellationToken
Returns
Task<JSReference> A reference to the new instance. Dispose it when the instance is no longer needed.
Exceptions
NodeJsException- The export is not a constructor, the constructor throws, or the Node.js process exits.
Remarks
The reference is bound to the host that ran the call. When the arguments contain a JSReference, the call runs on the host that keeps the referenced value.
DisposeAsync()
NodeJsHostPool.csStops all the Node.js processes. Pending calls fail with ObjectDisposedException.
net10.0
public ValueTask DisposeAsync()net11.0
public ValueTask DisposeAsync()Returns
EvaluateAsync overloads
EvaluateAsync(string, IReadOnlyList<JsonNode?>?, CancellationToken)
NodeJsHostPool.csEvaluates JavaScript code as the body of an async function that receives arguments.
public Task<JsonElement> EvaluateAsync(string code, IReadOnlyList<JsonNode?>? arguments, CancellationToken cancellationToken = default)Parameters
codestring- The body of the function. The arguments are available in the
argsarray. Usereturnto return a value andawaitto wait for promises.requireis available, and values stored onglobalThispersist across calls. argumentsIReadOnlyList<JsonNode?>?- The arguments, available in the
argsarray. Pass values as arguments instead of inserting them in the code, so they cannot change the code. UseJSValuefor values that JSON cannot represent, andJSReferenceto pass a value kept in the Node.js process. cancellationTokenCancellationToken- A token to stop waiting for the result. The JavaScript code keeps running.
Returns
Task<JsonElement> The JSON representation of the returned value. BigInt values are exact JSON numbers (Node.js 21 or later).
Exceptions
NodeJsException- The JavaScript code throws, or the Node.js process exits.
Remarks
When the arguments contain a JSReference, the call runs on the host that keeps the referenced value.
EvaluateAsync(string, CancellationToken)
NodeJsHostPool.csEvaluates JavaScript code as the body of an async function.
public Task<JsonElement> EvaluateAsync(string code, CancellationToken cancellationToken = default)Parameters
codestring- The body of the function. Use
returnto return a value andawaitto wait for promises.requireis available, and values stored onglobalThispersist across calls. cancellationTokenCancellationToken- A token to stop waiting for the result. The JavaScript code keeps running.
Returns
Task<JsonElement> The JSON representation of the returned value. BigInt values are exact JSON numbers (Node.js 21 or later).
Exceptions
NodeJsException- The JavaScript code throws, or the Node.js process exits.
EvaluateAsync<T>(string, IReadOnlyList<JsonNode?>?, JsonTypeInfo<T>, CancellationToken)
NodeJsHostPool.csEvaluates JavaScript code as the body of an async function that receives arguments, and deserializes the result.
public Task<T?> EvaluateAsync<T>(string code, IReadOnlyList<JsonNode?>? arguments, JsonTypeInfo<T> resultTypeInfo, CancellationToken cancellationToken = default)Type parameters
T
Parameters
codestring- The body of the function. The arguments are available in the
argsarray. Usereturnto return a value andawaitto wait for promises.requireis available, and values stored onglobalThispersist across calls. argumentsIReadOnlyList<JsonNode?>?- The arguments, available in the
argsarray. Pass values as arguments instead of inserting them in the code, so they cannot change the code. UseJSValuefor values that JSON cannot represent, andJSReferenceto pass a value kept in the Node.js process. resultTypeInfoJsonTypeInfo<T>- A token to stop waiting for the result. The JavaScript code keeps running.
cancellationTokenCancellationToken
Returns
Task<T?> The JSON representation of the returned value. BigInt values are exact JSON numbers (Node.js 21 or later).
Exceptions
JsonException- The result cannot be deserialized to
T.
Remarks
When the arguments contain a JSReference, the call runs on the host that keeps the referenced value.
EvaluateAsync<T>(string, object?[]?, JsonSerializerOptions?, CancellationToken)
NodeJsHostPool.csEvaluates JavaScript code as the body of an async function that receives arguments, and deserializes the 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<T?> EvaluateAsync<T>(string code, object?[]? arguments, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)Type parameters
T
Parameters
codestring- The body of the function. The arguments are available in the
argsarray. Usereturnto return a value andawaitto wait for promises.requireis available, and values stored onglobalThispersist across calls. argumentsobject?[]?- The arguments, available in the
argsarray. Pass values as arguments instead of inserting them in the code, so they cannot change the code. UseJSValuefor values that JSON cannot represent, andJSReferenceto pass a value kept in the Node.js process. optionsJsonSerializerOptions?- A token to stop waiting for the result. The JavaScript code keeps running.
cancellationTokenCancellationToken
Returns
Task<T?> The JSON representation of the returned value. BigInt values are exact JSON numbers (Node.js 21 or later).
Exceptions
JsonException- The result cannot be deserialized to
T.
Remarks
When the arguments contain a JSReference, the call runs on the host that keeps the referenced value.
EvaluateAsync<T>(string, JsonSerializerOptions?, CancellationToken)
NodeJsHostPool.csEvaluates JavaScript code as the body of an async function and deserializes the result 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?> EvaluateAsync<T>(string code, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)Type parameters
T
Parameters
codestring- The body of the function. Use
returnto return a value andawaitto wait for promises.requireis available, and values stored onglobalThispersist across calls. optionsJsonSerializerOptions?- A token to stop waiting for the result. The JavaScript code keeps running.
cancellationTokenCancellationToken
Returns
Task<T?> The JSON representation of the returned value. BigInt values are exact JSON numbers (Node.js 21 or later).
Exceptions
JsonException- The result cannot be deserialized to
T.
EvaluateAsync<T>(string, JsonTypeInfo<T>, CancellationToken)
NodeJsHostPool.csEvaluates JavaScript code as the body of an async function and deserializes the result.
public Task<T?> EvaluateAsync<T>(string code, JsonTypeInfo<T> resultTypeInfo, CancellationToken cancellationToken = default)Type parameters
T
Parameters
codestring- The body of the function. Use
returnto return a value andawaitto wait for promises.requireis available, and values stored onglobalThispersist across calls. resultTypeInfoJsonTypeInfo<T>- A token to stop waiting for the result. The JavaScript code keeps running.
cancellationTokenCancellationToken
Returns
Task<T?> The JSON representation of the returned value. BigInt values are exact JSON numbers (Node.js 21 or later).
Exceptions
JsonException- The result cannot be deserialized to
T.
EvaluateAsync<T>(string, CancellationToken)
NodeJsHostPool.csEvaluates JavaScript code as the body of an async function and deserializes the result 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?> EvaluateAsync<T>(string code, CancellationToken cancellationToken)Type parameters
T
Parameters
codestring- The body of the function. Use
returnto return a value andawaitto wait for promises.requireis available, and values stored onglobalThispersist across calls. cancellationTokenCancellationToken- A token to stop waiting for the result. The JavaScript code keeps running.
Returns
Task<T?> The JSON representation of the returned value. BigInt values are exact JSON numbers (Node.js 21 or later).
Exceptions
JsonException- The result cannot be deserialized to
T.
EvaluateReferenceAsync overloads
EvaluateReferenceAsync(string, IReadOnlyList<JsonNode?>?, CancellationToken)
NodeJsHostPool.csEvaluates JavaScript code as the body of an async function that receives arguments, and keeps the result in the Node.js process.
public Task<JSReference> EvaluateReferenceAsync(string code, IReadOnlyList<JsonNode?>? arguments, CancellationToken cancellationToken = default)Parameters
codestring- The body of the function. The arguments are available in the
argsarray. Usereturnto return a value andawaitto wait for promises.requireis available, and values stored onglobalThispersist across calls. argumentsIReadOnlyList<JsonNode?>?- The arguments, available in the
argsarray. Pass values as arguments instead of inserting them in the code, so they cannot change the code. UseJSValuefor values that JSON cannot represent, andJSReferenceto pass a value kept in the Node.js process. cancellationTokenCancellationToken- A token to stop waiting for the result. The JavaScript code keeps running.
Returns
Task<JSReference> A reference to the returned value. Dispose it when the value is no longer needed.
Exceptions
NodeJsException- The JavaScript code throws, or the Node.js process exits.
Remarks
The reference is bound to the host that ran the call. When the arguments contain a JSReference, the call runs on the host that keeps the referenced value.
EvaluateReferenceAsync(string, object?[]?, JsonSerializerOptions?, CancellationToken)
NodeJsHostPool.csEvaluates JavaScript code as the body of an async function that receives arguments, and keeps the result 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> EvaluateReferenceAsync(string code, object?[]? arguments, JsonSerializerOptions? options, CancellationToken cancellationToken = default)Parameters
codestring- The body of the function. The arguments are available in the
argsarray. Usereturnto return a value andawaitto wait for promises.requireis available, and values stored onglobalThispersist across calls. argumentsobject?[]?- The arguments, available in the
argsarray. Pass values as arguments instead of inserting them in the code, so they cannot change the code. UseJSValuefor values that JSON cannot represent, andJSReferenceto pass a value kept in the Node.js process. optionsJsonSerializerOptions?- A token to stop waiting for the result. The JavaScript code keeps running.
cancellationTokenCancellationToken
Returns
Task<JSReference> A reference to the returned value. Dispose it when the value is no longer needed.
Exceptions
NodeJsException- The JavaScript code throws, or the Node.js process exits.
Remarks
The reference is bound to the host that ran the call. When the arguments contain a JSReference, the call runs on the host that keeps the referenced value.
EvaluateReferenceAsync(string, CancellationToken)
NodeJsHostPool.csEvaluates JavaScript code as the body of an async function, and keeps the result in the Node.js process.
public Task<JSReference> EvaluateReferenceAsync(string code, CancellationToken cancellationToken = default)Parameters
codestring- The body of the function. Use
returnto return a value andawaitto wait for promises.requireis available, and values stored onglobalThispersist across calls. cancellationTokenCancellationToken- A token to stop waiting for the result. The JavaScript code keeps running.
Returns
Task<JSReference> A reference to the returned value. Dispose it when the value is no longer needed.
Exceptions
NodeJsException- The JavaScript code throws, or the Node.js process exits.
Remarks
The reference is bound to the host that ran the call. Calls of the pool whose arguments contain it run on that host.
EvaluateVoidAsync overloads
EvaluateVoidAsync(string, IReadOnlyList<JsonNode?>?, CancellationToken)
NodeJsHostPool.csEvaluates JavaScript code as the body of an async function that receives arguments, ignoring its result. The result does not need to be serializable.
public Task EvaluateVoidAsync(string code, IReadOnlyList<JsonNode?>? arguments, CancellationToken cancellationToken = default)Parameters
codestring- The body of the function. The arguments are available in the
argsarray. Usereturnto return a value andawaitto wait for promises.requireis available, and values stored onglobalThispersist across calls. argumentsIReadOnlyList<JsonNode?>?- The arguments, available in the
argsarray. Pass values as arguments instead of inserting them in the code, so they cannot change the code. UseJSValuefor values that JSON cannot represent, andJSReferenceto pass a value kept in the Node.js process. cancellationTokenCancellationToken- A token to stop waiting for the result. The JavaScript code keeps running.
Returns
Task The JSON representation of the returned value. BigInt values are exact JSON numbers (Node.js 21 or later).
Exceptions
NodeJsException- The JavaScript code throws, or the Node.js process exits.
Remarks
When the arguments contain a JSReference, the call runs on the host that keeps the referenced value.
EvaluateVoidAsync(string, object?[]?, JsonSerializerOptions?, CancellationToken)
NodeJsHostPool.csEvaluates JavaScript code as the body of an async function that receives arguments, ignoring 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 EvaluateVoidAsync(string code, object?[]? arguments, JsonSerializerOptions? options, CancellationToken cancellationToken = default)Parameters
codestring- The body of the function. The arguments are available in the
argsarray. Usereturnto return a value andawaitto wait for promises.requireis available, and values stored onglobalThispersist across calls. argumentsobject?[]?- The arguments, available in the
argsarray. Pass values as arguments instead of inserting them in the code, so they cannot change the code. UseJSValuefor values that JSON cannot represent, andJSReferenceto pass a value kept in the Node.js process. optionsJsonSerializerOptions?- A token to stop waiting for the result. The JavaScript code keeps running.
cancellationTokenCancellationToken
Returns
Task The JSON representation of the returned value. BigInt values are exact JSON numbers (Node.js 21 or later).
Exceptions
NodeJsException- The JavaScript code throws, or the Node.js process exits.
Remarks
When the arguments contain a JSReference, the call runs on the host that keeps the referenced value.
EvaluateVoidAsync(string, CancellationToken)
NodeJsHostPool.csEvaluates JavaScript code as the body of an async function, ignoring its result. The result does not need to be serializable.
public Task EvaluateVoidAsync(string code, CancellationToken cancellationToken = default)Parameters
codestring- The body of the function. Use
returnto return a value andawaitto wait for promises.requireis available, and values stored onglobalThispersist across calls. cancellationTokenCancellationToken- A token to stop waiting for the result. The JavaScript code keeps running.
Returns
Task The JSON representation of the returned value. BigInt values are exact JSON numbers (Node.js 21 or later).
Exceptions
NodeJsException- The JavaScript code throws, or the Node.js process exits.
InvokeAsync overloads
InvokeAsync(string, string?, IReadOnlyList<JsonNode?>?, CancellationToken)
NodeJsHostPool.csImports a module and calls one of its exports.
public Task<JsonElement> InvokeAsync(string module, string? exportName, IReadOnlyList<JsonNode?>? arguments = null, CancellationToken cancellationToken = default)Parameters
modulestring- The module specifier, as used by
import(): an npm package name, a relative path starting with./or../and including the file extension, resolved fromNodeJsHostOptions.WorkingDirectory, an absolute path, or afile:ornode:URL. A module is loaded once, and kept for the lifetime of the process. exportNamestring?- The name of the export. When
null, the default export is used. CommonJS exports are supported. argumentsIReadOnlyList<JsonNode?>?- The arguments passed to the function. Use
JSValuefor values that JSON cannot represent, andJSReferenceto pass a value kept in the Node.js process. cancellationTokenCancellationToken- 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), or the value of the export when it is not a function. BigInt values are exact JSON numbers (Node.js 21 or later).
Exceptions
NodeJsException- The JavaScript code throws, or the Node.js process exits.
Remarks
When the arguments contain a JSReference, the call runs on the host that keeps the referenced value.
InvokeAsync<T>(string, string?, IReadOnlyList<JsonNode?>?, JsonTypeInfo<T>, CancellationToken)
NodeJsHostPool.csImports a module, calls one of its exports, and deserializes the result.
public Task<T?> InvokeAsync<T>(string module, string? exportName, IReadOnlyList<JsonNode?>? arguments, JsonTypeInfo<T> resultTypeInfo, CancellationToken cancellationToken = default)Type parameters
T
Parameters
modulestring- The module specifier, as used by
import(): an npm package name, a relative path starting with./or../and including the file extension, resolved fromNodeJsHostOptions.WorkingDirectory, an absolute path, or afile:ornode:URL. A module is loaded once, and kept for the lifetime of the process. exportNamestring?- The name of the export. When
null, the default export is used. CommonJS exports are supported. argumentsIReadOnlyList<JsonNode?>?- The arguments passed to the function. Use
JSValuefor values that JSON cannot represent, andJSReferenceto pass a value kept in the Node.js process. resultTypeInfoJsonTypeInfo<T>- A token to stop waiting for the result. The JavaScript code keeps running.
cancellationTokenCancellationToken
Returns
Task<T?> The JSON representation of the value returned by the function (awaited if it is a promise), or the value of the export when it is not a function. BigInt values are exact JSON numbers (Node.js 21 or later).
Exceptions
JsonException- The result cannot be deserialized to
T.
Remarks
When the arguments contain a JSReference, the call runs on the host that keeps the referenced value.
InvokeAsync<T>(string, string?, object?[]?, JsonSerializerOptions?, CancellationToken)
NodeJsHostPool.csImports a module, calls one of its exports, and deserializes the 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<T?> InvokeAsync<T>(string module, string? exportName, object?[]? arguments = null, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)Type parameters
T
Parameters
modulestring- The module specifier, as used by
import(): an npm package name, a relative path starting with./or../and including the file extension, resolved fromNodeJsHostOptions.WorkingDirectory, an absolute path, or afile:ornode:URL. A module is loaded once, and kept for the lifetime of the process. exportNamestring?- The name of the export. When
null, the default export is used. CommonJS exports are supported. argumentsobject?[]?- The arguments passed to the function. Use
JSValuefor values that JSON cannot represent, andJSReferenceto pass a value kept in the Node.js process. optionsJsonSerializerOptions?- A token to stop waiting for the result. The JavaScript code keeps running.
cancellationTokenCancellationToken
Returns
Task<T?> The JSON representation of the value returned by the function (awaited if it is a promise), or the value of the export when it is not a function. BigInt values are exact JSON numbers (Node.js 21 or later).
Exceptions
JsonException- The result cannot be deserialized to
T.
Remarks
When the arguments contain a JSReference, the call runs on the host that keeps the referenced value.
InvokeAsync<T>(string, string?, CancellationToken)
NodeJsHostPool.csImports a module, calls one of its exports without arguments, 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 module, string? exportName, CancellationToken cancellationToken)Type parameters
T
Parameters
modulestring- The module specifier, as used by
import(): an npm package name, a relative path starting with./or../and including the file extension, resolved fromNodeJsHostOptions.WorkingDirectory, an absolute path, or afile:ornode:URL. A module is loaded once, and kept for the lifetime of the process. exportNamestring?- The name of the export. When
null, the default export is used. CommonJS exports are supported. cancellationTokenCancellationToken- The arguments passed to the function. Use
JSValuefor values that JSON cannot represent, andJSReferenceto pass a value kept in the Node.js process.
Returns
Task<T?> The JSON representation of the value returned by the function (awaited if it is a promise), or the value of the export when it is not a function. BigInt values are exact JSON numbers (Node.js 21 or later).
Exceptions
JsonException- The result cannot be deserialized to
T.
InvokeReferenceAsync overloads
InvokeReferenceAsync(string, string?, IReadOnlyList<JsonNode?>?, CancellationToken)
NodeJsHostPool.csImports a module, calls one of its exports, and keeps the result in the Node.js process.
public Task<JSReference> InvokeReferenceAsync(string module, string? exportName, IReadOnlyList<JsonNode?>? arguments = null, CancellationToken cancellationToken = default)Parameters
modulestring- The module specifier, as used by
import(): an npm package name, a relative path starting with./or../and including the file extension, resolved fromNodeJsHostOptions.WorkingDirectory, an absolute path, or afile:ornode:URL. A module is loaded once, and kept for the lifetime of the process. exportNamestring?- The name of the export. When
null, the default export is used. CommonJS exports are supported. argumentsIReadOnlyList<JsonNode?>?- The arguments passed to the function. Use
JSValuefor values that JSON cannot represent, andJSReferenceto pass a value kept in the Node.js process. cancellationTokenCancellationToken- 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), or to the value of the export when it is not a function. Dispose it when the value is no longer needed.
Exceptions
NodeJsException- The JavaScript code throws, or the Node.js process exits.
Remarks
The reference is bound to the host that ran the call. Calls of the pool whose arguments contain it run on that host.
InvokeReferenceAsync(string, string?, object?[]?, JsonSerializerOptions?, CancellationToken)
NodeJsHostPool.csImports a module, calls one of its exports, and keeps the result 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> InvokeReferenceAsync(string module, string? exportName, object?[]? arguments, JsonSerializerOptions? options, CancellationToken cancellationToken = default)Parameters
modulestring- The module specifier, as used by
import(): an npm package name, a relative path starting with./or../and including the file extension, resolved fromNodeJsHostOptions.WorkingDirectory, an absolute path, or afile:ornode:URL. A module is loaded once, and kept for the lifetime of the process. exportNamestring?- The name of the export. When
null, the default export is used. CommonJS exports are supported. argumentsobject?[]?- The arguments passed to the function. Use
JSValuefor values that JSON cannot represent, andJSReferenceto pass a value kept in the Node.js process. optionsJsonSerializerOptions?- A token to stop waiting for the result. The JavaScript code keeps running.
cancellationTokenCancellationToken
Returns
Task<JSReference> A reference to the value returned by the function (awaited if it is a promise), or to the value of the export when it is not a function. Dispose it when the value is no longer needed.
Exceptions
NodeJsException- The JavaScript code throws, or the Node.js process exits.
Remarks
The reference is bound to the host that ran the call. Calls of the pool whose arguments contain it run on that host.
InvokeVoidAsync overloads
InvokeVoidAsync(string, string?, IReadOnlyList<JsonNode?>?, CancellationToken)
NodeJsHostPool.csImports a module and calls one of its exports, ignoring its result. The result does not need to be serializable.
public Task InvokeVoidAsync(string module, string? exportName, IReadOnlyList<JsonNode?>? arguments = null, CancellationToken cancellationToken = default)Parameters
modulestring- The module specifier, as used by
import(): an npm package name, a relative path starting with./or../and including the file extension, resolved fromNodeJsHostOptions.WorkingDirectory, an absolute path, or afile:ornode:URL. A module is loaded once, and kept for the lifetime of the process. exportNamestring?- The name of the export. When
null, the default export is used. CommonJS exports are supported. argumentsIReadOnlyList<JsonNode?>?- The arguments passed to the function. Use
JSValuefor values that JSON cannot represent, andJSReferenceto pass a value kept in the Node.js process. cancellationTokenCancellationToken- 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), or the value of the export when it is not a function. BigInt values are exact JSON numbers (Node.js 21 or later).
Exceptions
NodeJsException- The JavaScript code throws, or the Node.js process exits.
Remarks
When the arguments contain a JSReference, the call runs on the host that keeps the referenced value.
InvokeVoidAsync(string, string?, object?[]?, JsonSerializerOptions?, CancellationToken)
NodeJsHostPool.csImports a module and calls one of its exports, ignoring 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 module, string? exportName, object?[]? arguments, JsonSerializerOptions? options, CancellationToken cancellationToken = default)Parameters
modulestring- The module specifier, as used by
import(): an npm package name, a relative path starting with./or../and including the file extension, resolved fromNodeJsHostOptions.WorkingDirectory, an absolute path, or afile:ornode:URL. A module is loaded once, and kept for the lifetime of the process. exportNamestring?- The name of the export. When
null, the default export is used. CommonJS exports are supported. argumentsobject?[]?- The arguments passed to the function. Use
JSValuefor values that JSON cannot represent, andJSReferenceto pass a value kept in the Node.js process. optionsJsonSerializerOptions?- A token to stop waiting for the result. The JavaScript code keeps running.
cancellationTokenCancellationToken
Returns
Task The JSON representation of the value returned by the function (awaited if it is a promise), or the value of the export when it is not a function. BigInt values are exact JSON numbers (Node.js 21 or later).
Exceptions
NodeJsException- The JavaScript code throws, or the Node.js process exits.
Remarks
When the arguments contain a JSReference, the call runs on the host that keeps the referenced value.
RunAsync overloads
RunAsync(Func<NodeJsHost, Task>, CancellationToken)
NodeJsHostPool.csRuns a function with one host of the pool. Use it to run several calls on the same Node.js process.
public Task RunAsync(Func<NodeJsHost, Task> action, CancellationToken cancellationToken = default)Parameters
actionFunc<NodeJsHost, Task>- The function to run. The host must not be used after the returned task completes.
cancellationTokenCancellationToken- A token to stop waiting for a host to be available.
Returns
RunAsync<T>(Func<NodeJsHost, Task<T>>, CancellationToken)
NodeJsHostPool.csRuns a function with one host of the pool. Use it to run several calls on the same Node.js process.
net10.0
public Task<T> RunAsync<T>(Func<NodeJsHost, Task<T>> action, CancellationToken cancellationToken = default)net11.0
public Task<T> RunAsync<T>(Func<NodeJsHost, Task<T>> action, CancellationToken cancellationToken = default)Type parameters
T
Parameters
actionFunc<NodeJsHost, Task<T>>- The function to run. The host must not be used after the returned task completes.
cancellationTokenCancellationToken- A token to stop waiting for a host to be available.
Returns
Remarks
Calls made on the pool by the function, instead of on the host, do not wait for NodeJsHostOptions.MaxConcurrentCalls: the function already counts as a call, and waiting would never end when all the calls of the pool are such functions.
StartAsync(int, NodeJsHostOptions?, CancellationToken)
NodeJsHostPool.csStarts a pool of Node.js processes.
net10.0
public static Task<NodeJsHostPool> StartAsync(int size, NodeJsHostOptions? options = null, CancellationToken cancellationToken = default)net11.0
public static Task<NodeJsHostPool> StartAsync(int size, NodeJsHostOptions? options = null, CancellationToken cancellationToken = default)Parameters
sizeint- The number of Node.js processes.
Environment.ProcessorCountis a good default for CPU-bound code. optionsNodeJsHostOptions?- The options used to start each process. They are copied, so changing them once the pool is started has no effect, including on the processes that replace the ones that exit.
cancellationTokenCancellationToken- A token to cancel the startup.
Returns
Exceptions
NodeJsException- A Node.js process fails to start.
ArgumentOutOfRangeExceptionsize,NodeJsHostOptions.MaxConcurrentCalls,NodeJsHostOptions.StartupTimeout,NodeJsHostOptions.UnresponsiveTimeout, orNodeJsHostOptions.WorkerThreadsis zero or negative.