SimpleMessageBus 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package SimpleMessageBus --version 1.0.0                
NuGet\Install-Package SimpleMessageBus -Version 1.0.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="SimpleMessageBus" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SimpleMessageBus --version 1.0.0                
#r "nuget: SimpleMessageBus, 1.0.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.
// Install SimpleMessageBus as a Cake Addin
#addin nuget:?package=SimpleMessageBus&version=1.0.0

// Install SimpleMessageBus as a Cake Tool
#tool nuget:?package=SimpleMessageBus&version=1.0.0                

SimpleMessageBus

Offers simple intra-app pub-sub messaging.


I needed a quick-and-dirty message-bus, and found a dearth of suitable NuGet libraries available. I just needed a simple, in-memory message-bus.

  • No persistent queues
  • No dead-letter handling
  • No distributed-processing capability

This project was created to answer that need.

Sample Usage

Basic Usage

public class MyMessage : IMessage { }

//
// Subscribe to the shared (singleton) MessageBus instance,
// receiving messages of a given type.
//
// Messages are delivered synchronously (i.e., on the same
// thread as the publisher).
//
MessageBus.When<MyMessage>()
    .FromAny()
    .Then(m => Console.WriteLine("Received a message!"));

//
// Publish a message.
//
MessageBus.Shared.Publish(new MyMessage());

Receive Only Matching Message-Types

public class GeneralMessage : IMessage { }
public class SpecificMessage : GeneralMessage { }

//
// Subscribe to the shared MessageBus,
// receiving messages *exactly* of the given type.
//
MessageBus.When<GeneralMessage>(AllowSubclasses: false)
    .FromAny()
    .Then(m => Console.WriteLine("..."));

//
// This will be ignored.
MessageBus.Shared.Publish(new SpecificMessage());
//
// This will be received.
MessageBus.Shared.Publish(new GeneralMessage());

Publish/Subscribe to Specific Topic

//
// Messages may be published to specific Topics,
// where a Topic is any object (not just a string).
//
public class MyMessage : IMessage { }

MessageBus.When<GeneralMessage>()
    .From("my-topic")
    .Then(m => Console.WriteLine("check!"));

//
// This will be ignored.
MessageBus.Shared.Publish(new MyMessage());
//
//This will be received.
MessageBus.Shared.Publish("my-topic", new MyMessage());

Asynchronous Delivery

public class MyMessage : IMessage { }

//
// Delivery is specified by the subscriber, not the publisher.
MessageBus.Shared.When<MyMessage>()
    .FromAny()
    .Then(m => Console.WriteLine("asynchronous!"), Synchronous: false);

//
// Publishing is performed same as always:
MessageBus.Shared.Publish(new MyMessage());

Message Pooling

//
// Message-types that extend IPoolableMessage can be
// pooled, allowing us to avoid garbage-collection.
// This may be useful when performing high-throughput,
// low-footprint messaging.
//
public class MyMessage : IPoolableMessage {
    public int Value { get; set; }
    //
    // All pooled messages must be able to "reset" themselves
    // to a blank/fresh state.
    public void Reset() => Value = 0;
}

MessageBus.When<MyMessage>()
    .FromAny()
    .Then(m => Console.WriteLine($"Value = {m.Value}"));

//
// We do not explicitly create pooled messages,
// but obtain an instance from the MessageBus.
MessageBus.Shared.Publish<MyMessage>(m => m.Value = 1);

//
// We can, of course, publish pooled messages to a topic.
MessageBus.Shared.Publish<MyMessage>("my-topic", m => m.Value = 1);

Instance (Non-Singleton) MessageBus

//
// We can create a new MessageBus either with default settings ...
var bus = new MessageBus();

//
// ... or with customized settings.
bus = new MessageBus(settings => {
    settings.ObjectPoolSize = 16;
});
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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. 
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
1.0.8 557 10/26/2023
1.0.5 409 10/26/2023
1.0.2 450 10/26/2023
1.0.1 432 10/26/2023
1.0.0 429 10/26/2023