Soenneker.Blazor.Utils.BlazorInvoker
4.0.1053
Prefix Reserved
dotnet add package Soenneker.Blazor.Utils.BlazorInvoker --version 4.0.1053
NuGet\Install-Package Soenneker.Blazor.Utils.BlazorInvoker -Version 4.0.1053
<PackageReference Include="Soenneker.Blazor.Utils.BlazorInvoker" Version="4.0.1053" />
<PackageVersion Include="Soenneker.Blazor.Utils.BlazorInvoker" Version="4.0.1053" />
<PackageReference Include="Soenneker.Blazor.Utils.BlazorInvoker" />
paket add Soenneker.Blazor.Utils.BlazorInvoker --version 4.0.1053
#r "nuget: Soenneker.Blazor.Utils.BlazorInvoker, 4.0.1053"
#:package Soenneker.Blazor.Utils.BlazorInvoker@4.0.1053
#addin nuget:?package=Soenneker.Blazor.Utils.BlazorInvoker&version=4.0.1053
#tool nuget:?package=Soenneker.Blazor.Utils.BlazorInvoker&version=4.0.1053
Soenneker.Blazor.Utils.BlazorInvoker
A small adapter that exposes a Func<TInput, ValueTask> as an instance [JSInvokable] method for JavaScript-to-.NET callbacks.
Installation
dotnet add package Soenneker.Blazor.Utils.BlazorInvoker
There is no service registration. Create an invoker for a specific callback, wrap it in a DotNetObjectReference, and pass that reference to your JavaScript module.
Component example
@using Microsoft.JSInterop
@using Soenneker.Blazor.Utils.BlazorInvoker
@implements IAsyncDisposable
@inject IJSRuntime JS
<p>@_message</p>
@code {
private IJSObjectReference? _module;
private BlazorInvoker<BrowserMessage>? _invoker;
private DotNetObjectReference<BlazorInvoker<BrowserMessage>>? _reference;
private string? _message;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (!firstRender)
return;
_invoker = new BlazorInvoker<BrowserMessage>(message =>
new ValueTask(InvokeAsync(() =>
{
_message = message.Text;
StateHasChanged();
})));
_reference = DotNetObjectReference.Create(_invoker);
_module = await JS.InvokeAsync<IJSObjectReference>("import", "./callback.js");
await _module.InvokeVoidAsync("registerCallback", _reference);
}
public async ValueTask DisposeAsync()
{
if (_module is not null)
{
await _module.InvokeVoidAsync("unregisterCallback");
await _module.DisposeAsync();
}
_reference?.Dispose();
}
private sealed record BrowserMessage(string Text);
}
The JavaScript side receives the object reference and invokes its Invoke method:
let callback;
export function registerCallback(reference) {
callback = reference;
}
export function unregisterCallback() {
callback = null;
}
export async function publish(text) {
if (!callback)
throw new Error("The .NET callback has not been registered.");
await callback.invokeMethodAsync("Invoke", { text });
}
Behavior and ownership
Invoke(args)awaits the configured delegate. A delegate exception rejects JavaScript'sinvokeMethodAsyncpromise, so JavaScript callers should await and handle failures.- The invoker does not marshal work onto a component renderer. Use the owning component's
InvokeAsyncwhen the delegate changes component state or callsStateHasChanged. - JavaScript cannot supply a .NET
CancellationTokento this method. Capture a component or operation token in the delegate when cancellation is required. - The invoker does not create or dispose
DotNetObjectReference. The owner must retain and dispose that reference, or the delegate and any captured component state remain rooted. - A disposed object reference must not be invoked again. Unregister browser listeners before disposal when they can race with component teardown.
- Treat callback payloads as untrusted input. Validate values, sizes, identifiers, and authorization before using them in privileged operations.
BlazorInvoker<TInput> is useful when several interop wrappers need the same one-argument callback shape. For a component with multiple named callbacks, ordinary [JSInvokable] instance methods may be clearer.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Microsoft.JSInterop (>= 10.0.11)
- Soenneker.Extensions.ValueTask (>= 4.0.120)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Soenneker.Blazor.Utils.BlazorInvoker:
| Package | Downloads |
|---|---|
|
Soenneker.Blazor.Utils.InteropEventListener
Manages the registration, removal, and disposal of .NET object references used for interop event listeners. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 4.0.1053 | 322 | 8/30/2026 |
| 4.0.1052 | 176 | 8/30/2026 |
| 4.0.1051 | 35 | 8/29/2026 |
| 4.0.1050 | 41 | 8/29/2026 |
| 4.0.1049 | 32 | 8/29/2026 |
| 4.0.1048 | 43 | 8/29/2026 |
| 4.0.1047 | 3,940 | 8/12/2026 |
| 4.0.1046 | 4,317 | 7/28/2026 |
| 4.0.1045 | 194 | 7/27/2026 |
| 4.0.1044 | 3,996 | 7/16/2026 |
| 4.0.1042 | 1,235 | 7/14/2026 |
| 4.0.1041 | 4,793 | 6/18/2026 |
| 4.0.1039 | 2,989 | 6/9/2026 |
| 4.0.1038 | 2,337 | 6/5/2026 |
| 4.0.1037 | 209 | 6/5/2026 |
| 4.0.1036 | 113 | 6/5/2026 |
| 4.0.1035 | 3,133 | 5/12/2026 |
| 4.0.1034 | 4,097 | 4/22/2026 |
| 4.0.1033 | 144 | 4/21/2026 |
| 4.0.1032 | 2,110 | 4/14/2026 |
Update dependency Soenneker.Extensions.ValueTask to 4.0.120 (#1584)