ByteAether.WeakEvent 1.1.0

Prefix Reserved
dotnet add package ByteAether.WeakEvent --version 1.1.0
                    
NuGet\Install-Package ByteAether.WeakEvent -Version 1.1.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="ByteAether.WeakEvent" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ByteAether.WeakEvent" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="ByteAether.WeakEvent" />
                    
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 ByteAether.WeakEvent --version 1.1.0
                    
#r "nuget: ByteAether.WeakEvent, 1.1.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 ByteAether.WeakEvent@1.1.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=ByteAether.WeakEvent&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=ByteAether.WeakEvent&version=1.1.0
                    
Install as a Cake Tool

WeakEvent

from ByteAether

License NuGet Version NuGet Downloads GitHub Build Status GitHub Security

WeakEvent is a lightweight .NET implementation of the weak event pattern. A normal event can keep an inactive subscriber alive because the publisher holds a reference to its handler. WeakEvent breaks that retention by storing instance handlers with weak references, allowing subscriber objects to be reclaimed when no longer used elsewhere, even if they have not explicitly unsubscribed.

The library fits applications where long-lived publishers notify short-lived or repeatedly created subscribers, such as UI components, desktop applications, background services, and Blazor components. It supports synchronous and asynchronous handlers, notifications with or without payload data, and cancellation-aware publishing. Static handlers remain alive for the lifetime of the event source, while dead instance subscriptions are ignored and cleaned up during publication.

Key Features

.NET Standard 2.0

  • Weak References: Subscribers are held weakly so they can be garbage-collected when no longer in use.
  • Events With or Without Payloads: Use WeakEvent for notifications or WeakEvent<TEvent> to pass event data.
  • Synchronous and Asynchronous Handlers: Subscribe with Action, Func<Task>, or a cancellation-aware async delegate.
  • Automatic Cleanup: Dead subscribers are removed during publication.
  • Simple API: Methods for subscribing, unsubscribing, and publishing.

Publication behavior

  • Subscribers are invoked sequentially in subscription order.
  • Publication uses a subscriber snapshot captured when it starts. Subscribing or unsubscribing during publication affects later publications, not the current one.
  • Cancellation is checked before publication starts and before each subscriber is invoked. Cancellation-aware handlers receive the token passed to PublishAsync.
  • Static handlers remain alive for the lifetime of the event source. Instance handlers, including capturing lambdas, are held weakly.
  • Synchronous handler exceptions propagate unchanged. Asynchronous handler exceptions propagate after the returned task is awaited.
  • Each invocation in a multicast delegate is subscribed independently and can be unsubscribed as a group.

Installation

Install via NuGet:

dotnet add package ByteAether.WeakEvent

Use the --version option to specify a preview version to install.

Usage

Using the WeakEvent

using ByteAether.WeakEvent;

// Create an event without payload data.
var myEvent = new WeakEvent();

// Create and subscribe a handler.
var subscriber = () => Console.WriteLine("Event received!");
myEvent.Subscribe(subscriber);

// Publish the event.
await myEvent.PublishAsync();

Using the WeakEvent<TEvent>

using ByteAether.WeakEvent;

// Create an event with payload data.
var myEvent = new WeakEvent<MyEventData>();

// Create and subscribe a handler.
var subscriber = (MyEventData data) => Console.WriteLine("Received: " + data.Message);
myEvent.Subscribe(subscriber);

// Publish the event.
await myEvent.PublishAsync(new MyEventData("Hello, World!"));

// Define your event data
public record MyEventData(string Message);

API Overview

WeakEvent

  • SubscriberCount
    Number of live subscribers currently registered.
  • Subscribe(Action handler)
    Subscribe(Func<Task> handler)
    Subscribe(Func<CancellationToken, Task> handler)
    Subscribes the handler. Each invocation in a multicast delegate is subscribed independently. Live handlers are invoked when the event is published.
  • Unsubscribe(Action handler)
    Unsubscribe(Func<Task> handler)
    Unsubscribe(Func<CancellationToken, Task> handler)
    Unsubscribes the handler and returns whether at least one matching invocation was removed. A multicast delegate unsubscribes all matching invocations.
  • PublishAsync(CancellationToken cancellationToken = default)
    Invokes live subscribers sequentially and removes dead subscribers.

WeakEvent<TEvent>

  • SubscriberCount
    Number of live subscribers currently registered.
  • Subscribe(Action<TEvent> handler)
    Subscribe(Func<TEvent, Task> handler)
    Subscribe(Func<TEvent, CancellationToken, Task> handler)
    Subscribes the handler. Each invocation in a multicast delegate is subscribed independently. Live handlers are invoked when the event is published.
  • Unsubscribe(Action<TEvent> handler)
    Unsubscribe(Func<TEvent, Task> handler)
    Unsubscribe(Func<TEvent, CancellationToken, Task> handler)
    Unsubscribes the handler and returns whether at least one matching invocation was removed. A multicast delegate unsubscribes all matching invocations.
  • PublishAsync(TEvent eventData, CancellationToken cancellationToken = default)
    Invokes live subscribers sequentially with eventData and removes dead subscribers.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

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
1.1.0 0 11/23/2026
1.1.0-rc.0 0 11/23/2026
1.0.3 1,226 7/6/2025
1.0.2 303 6/19/2025
1.0.1 336 4/14/2025
1.0.0 278 3/18/2025
0.1.0 342 2/24/2025 0.1.0 is deprecated because it is no longer maintained.