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
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Soenneker.Blazor.Utils.BlazorInvoker" Version="4.0.1053" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Soenneker.Blazor.Utils.BlazorInvoker" Version="4.0.1053" />
                    
Directory.Packages.props
<PackageReference Include="Soenneker.Blazor.Utils.BlazorInvoker" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Soenneker.Blazor.Utils.BlazorInvoker --version 4.0.1053
                    
#r "nuget: Soenneker.Blazor.Utils.BlazorInvoker, 4.0.1053"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Soenneker.Blazor.Utils.BlazorInvoker@4.0.1053
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Soenneker.Blazor.Utils.BlazorInvoker&version=4.0.1053
                    
Install as a Cake Addin
#tool nuget:?package=Soenneker.Blazor.Utils.BlazorInvoker&version=4.0.1053
                    
Install as a Cake Tool

alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image

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's invokeMethodAsync promise, so JavaScript callers should await and handle failures.
  • The invoker does not marshal work onto a component renderer. Use the owning component's InvokeAsync when the delegate changes component state or calls StateHasChanged.
  • JavaScript cannot supply a .NET CancellationToken to 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
Loading failed

Update dependency Soenneker.Extensions.ValueTask to 4.0.120 (#1584)