Chaos.BlazorMessageBus 0.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Chaos.BlazorMessageBus --version 0.2.0
                    
NuGet\Install-Package Chaos.BlazorMessageBus -Version 0.2.0
                    
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="Chaos.BlazorMessageBus" Version="0.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Chaos.BlazorMessageBus" Version="0.2.0" />
                    
Directory.Packages.props
<PackageReference Include="Chaos.BlazorMessageBus" />
                    
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 Chaos.BlazorMessageBus --version 0.2.0
                    
#r "nuget: Chaos.BlazorMessageBus, 0.2.0"
                    
#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 Chaos.BlazorMessageBus@0.2.0
                    
#: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=Chaos.BlazorMessageBus&version=0.2.0
                    
Install as a Cake Addin
#tool nuget:?package=Chaos.BlazorMessageBus&version=0.2.0
                    
Install as a Cake Tool

BlazorMessageBus

A lightweight, thread-safe message bus for Blazor applications. BlazorMessageBus enables decoupled communication between components and services using a publish/subscribe pattern.

GitHub License NuGet Version NuGet Downloads GitHub last commit GitHub Actions Workflow Status

Features

  • Publish/Subscribe: Send messages of any type and subscribe with type-safe handlers.
  • Async and Sync Handlers: Supports both synchronous and asynchronous message handlers.
  • Scoped Message Exchange: Simplifies subscription management for Blazor components.
  • Thread-Safe: Safe for concurrent publishing and subscribing.
  • Flexible Error Handling: Configurable exception handling and fail-fast options.
  • Dependency Injection: Easy integration with ASP.NET Core DI.

Installation

Add the NuGet package:

dotnet add package Chaos.BlazorMessageBus

Getting Started

  1. Register the services in your Program.cs:
builder.Services.AddBlazorMessageBus(options =>
{
    options.StopOnFirstError = false;
    options.OnPublishException = ex => { /* log or handle */ return Task.CompletedTask; };
});
  1. Inject and use the message bus in your components or services:
@inject IBlazorMessageBus MessageBus

// Subscribe
var subscription = MessageBus.Subscribe<string>(msg => Console.WriteLine(msg));

// Publish
await MessageBus.PublishAsync("Hello, world!");

// Dispose when done
subscription.Dispose();
  1. For Blazor components, use IBlazorMessageExchange for automatic cleanup:
@inject IBlazorMessageExchange MessageExchange

protected override void OnInitialized()
{
    MessageExchange.Subscribe<string>(msg => { /* handle message */ });
}

public void Dispose()
{
    MessageExchange.Dispose();
}

Configuration Options

  • StopOnFirstError (Boolean): If true, publishing stops on the first handler exception. If false, all handlers are invoked and exceptions are aggregated.
  • OnPublishException (Func<Exception, Task>): Optional async handler for exceptions thrown by subscribers.

Advanced Usage

Handling Multiple Message Types

You can subscribe to and publish any type:

MessageBus.Subscribe<int>(i => Console.WriteLine($"Int: {i}"));
MessageBus.Subscribe<MyCustomEvent>(evt => HandleCustomEvent(evt));

await MessageBus.PublishAsync(42);
await MessageBus.PublishAsync(new MyCustomEvent { ... });

Asynchronous Handlers

MessageBus.Subscribe<string>(async msg =>
{
    await SomeAsyncOperation(msg);
});

Exception Handling and Aggregation

If multiple handlers throw exceptions during publish, all exceptions are aggregated and thrown as an AggregateException (unless StopOnFirstError is enabled):

try
{
    await MessageBus.PublishAsync("Test");
}
catch (AggregateException ex)
{
    foreach (var inner in ex.InnerExceptions)
    {
        // Handle each exception
    }
}

Thread Safety

BlazorMessageBus is thread-safe. You can safely publish and subscribe from multiple threads concurrently.

Unsubscribing

Dispose the returned subscription to unsubscribe:

var subscription = MessageBus.Subscribe<string>(...);
subscription.Dispose();

License

MIT License - see LICENSE for more information.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.3.0 137 8/10/2025
0.2.0 113 8/3/2025
0.1.1 50 8/2/2025
0.1.0 25 8/2/2025

# v0.2.0 (2025-08-03)

### 🐛 Bug Fixes

- Fix lifetimes for BlazorMessageBus and BlazorMessageExchange services (#23) by @chA0s-Chris