SimplyWorks.Bus 8.1.0

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

SimplyWorks.Bus

Build and Publish NuGet Package NuGet License: MIT

A lightweight .NET 8 message bus library built on top of RabbitMQ, designed to simplify publish-subscribe patterns and event-driven architectures in ASP.NET Core applications.

Features

  • ๐ŸšŒ Simple Message Publishing: Easy-to-use interfaces for publishing messages
  • ๐Ÿ“ก Broadcasting: Send messages to multiple consumers across different application instances
  • ๐Ÿ”„ Automatic Retries: Built-in retry mechanism with dead letter queues
  • ๐ŸŽฏ Typed Consumers: Strong-typed message consumers with IConsume<T>
  • ๐Ÿ“ป Event Listeners: Broadcast listeners with IListen<T> for cross-application communication
  • ๐Ÿ” JWT Integration: Built-in JWT token propagation for authenticated messaging
  • ๐Ÿ—๏ธ ASP.NET Core Integration: Seamless dependency injection and hosted service integration
  • ๐Ÿงช Testing Support: Mock publisher for unit testing scenarios

Installation

dotnet add package SimplyWorks.Bus

Quick Start

1. Configuration

Add RabbitMQ connection string to your appsettings.json:

{
  "ConnectionStrings": {
    "RabbitMQ": "amqp://guest:guest@localhost:5672/"
  }
}

2. Service Registration

In your Startup.cs or Program.cs:

// Basic bus setup
services.AddBus(config =>
{
    config.ApplicationName = "MyApp";
    // Optional JWT configuration
    config.Token.Key = Configuration["Token:Key"];
    config.Token.Issuer = Configuration["Token:Issuer"];
    config.Token.Audience = Configuration["Token:Audience"];
});

// For publishing messages
services.AddBusPublish();

// For consuming messages
services.AddBusConsume();

// For listening to broadcasts only
services.AddBusListen();

3. Publishing Messages

public class OrderController : ControllerBase
{
    private readonly IPublish _publisher;
    private readonly IBroadcast _broadcaster;

    public OrderController(IPublish publisher, IBroadcast broadcaster)
    {
        _publisher = publisher;
        _broadcaster = broadcaster;
    }

    [HttpPost]
    public async Task<IActionResult> CreateOrder(CreateOrderRequest request)
    {
        var order = new OrderCreated
        {
            OrderId = Guid.NewGuid(),
            CustomerId = request.CustomerId,
            Amount = request.Amount
        };

        // Publish to specific consumers
        await _publisher.Publish(order);

        // Broadcast to all listening applications
        await _broadcaster.Broadcast(new OrderNotification 
        { 
            Message = "New order created" 
        });

        return Ok();
    }
}

4. Consuming Messages

Create typed consumers by implementing IConsume<T>:

public class OrderCreatedConsumer : IConsume<OrderCreated>
{
    private readonly ILogger<OrderCreatedConsumer> _logger;
    private readonly IOrderService _orderService;

    public OrderCreatedConsumer(ILogger<OrderCreatedConsumer> logger, IOrderService orderService)
    {
        _logger = logger;
        _orderService = orderService;
    }

    public async Task Process(OrderCreated message)
    {
        _logger.LogInformation("Processing order {OrderId}", message.OrderId);
        await _orderService.ProcessOrder(message);
    }
}

5. Listening to Broadcasts

Create broadcast listeners by implementing IListen<T>:

public class OrderNotificationListener : IListen<OrderNotification>
{
    private readonly INotificationService _notificationService;

    public OrderNotificationListener(INotificationService notificationService)
    {
        _notificationService = notificationService;
    }

    public async Task Process(OrderNotification message)
    {
        await _notificationService.SendNotification(message.Message);
    }
}

6. String-based Consumers

For dynamic message types, implement IConsume:

public class GenericConsumer : IConsume
{
    public async Task<IEnumerable<string>> GetMessageTypeNames()
    {
        return new[] { "DynamicMessage1", "DynamicMessage2" };
    }

    public async Task Process(string messageTypeName, string message)
    {
        Console.WriteLine($"Received {messageTypeName}: {message}");
    }
}

Configuration Options

Bus Options

services.AddBus(config =>
{
    config.ApplicationName = "MyApplication";           // Required: Your application name
    config.DefaultQueuePrefetch = 1;                    // Messages to prefetch (default: 1)
    config.DefaultRetryCount = 3;                       // Retry attempts (default: 3)
    config.DefaultRetryAfter = 30000;                   // Retry delay in ms (default: 30s)
    config.HeartBeatTimeOut = 60;                       // RabbitMQ heartbeat timeout
    config.ListenRetryCount = 3;                        // Listener retry attempts
    config.ListenRetryAfter = 30;                       // Listener retry delay
    
    // JWT Token configuration (optional)
    config.Token.Key = "your-secret-key";
    config.Token.Issuer = "your-issuer";
    config.Token.Audience = "your-audience";
});

Queue Options

Customize queue behavior for specific message types:

services.AddBus(config =>
{
    config.Options["MyMessageType"] = new QueueOptions
    {
        RetryCount = 5,
        RetryAfter = 60000,
        QueuePrefetch = 10
    };
});

Testing

For unit testing, use the mock publisher:

// In your test setup
services.AddBusPublishMock();

// The mock publisher will log publish calls instead of sending to RabbitMQ

Advanced Features

Error Handling

Consumers can implement error handling methods:

public class OrderConsumer : IConsume<OrderCreated>
{
    public async Task Process(OrderCreated message)
    {
        // Main processing logic
    }

    public async Task OnFail(Exception exception, string rawMessage)
    {
        // Handle processing failures
        // This method is optional
    }
}

Message Context

Access request context and user information in consumers:

public class AuthenticatedConsumer : IConsume<SecureMessage>
{
    private readonly RequestContext _requestContext;

    public AuthenticatedConsumer(RequestContext requestContext)
    {
        _requestContext = requestContext;
    }

    public async Task Process(SecureMessage message)
    {
        var user = _requestContext.User; // Access the authenticated user
        var correlationId = _requestContext.CorrelationId; // Trace requests
    }
}

Consumer Refresh

Refresh consumers dynamically at runtime:

await _broadcaster.RefreshConsumers();

Architecture

SW.Bus uses RabbitMQ exchanges and queues with the following pattern:

  • Process Exchange: For direct message routing to specific consumers
  • Node Exchange: For broadcasting messages to all application instances
  • Dead Letter Exchanges: For failed message handling and retries
  • Retry Queues: Temporary storage for messages awaiting retry

Dependencies

  • .NET 8.0
  • RabbitMQ.Client 6.8.1
  • SimplyWorks.HttpExtensions 5.0.0
  • SimplyWorks.PrimitiveTypes 6.0.5
  • Scrutor 4.2.2 (for assembly scanning)

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

For issues and questions:

  • Create an issue on GitHub
  • Check the sample application in SW.Bus.SampleWeb for usage examples
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 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. 
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 SimplyWorks.Bus:

Package Downloads
ChequeScore.PrimitiveTypes.EFUtils

ChequeScore EF Utils

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
8.1.0 268 9/16/2025
8.0.11 1,792 1/27/2025
8.0.10 347 11/7/2024
8.0.9 147 10/20/2024
8.0.4 430 6/27/2024
8.0.3 154 6/27/2024
8.0.2 240 6/2/2024
8.0.1 265 5/7/2024
8.0.0 169 4/30/2024
6.0.24 2,121 12/16/2024
6.0.23 1,838 11/7/2024
6.0.22 782 10/17/2024
6.0.15 9,202 7/16/2023
6.0.12 2,562 4/11/2023
6.0.10 601 3/26/2023
6.0.8 331 3/23/2023
6.0.6 7,279 12/14/2022
6.0.4 3,763 11/24/2022
6.0.2 3,192 11/15/2022
6.0.0 5,264 7/7/2022
5.0.5 2,794 7/7/2022
5.0.3 534 7/7/2022
5.0.2 7,203 3/3/2022
5.0.1 1,263 10/23/2021
3.0.26 3,835 8/29/2021
3.0.25 682 8/24/2021
3.0.23 755 3/3/2021
3.0.18 838 2/4/2021
3.0.15 4,191 12/15/2020
3.0.11 3,727 11/10/2020
3.0.8 866 10/26/2020
3.0.6 572 10/26/2020
3.0.4 521 10/26/2020
3.0.2 578 10/1/2020
3.0.1 578 10/1/2020
2.0.7 426 8/24/2021
2.0.6 764 9/30/2020
2.0.5 520 9/30/2020
2.0.4 1,170 8/20/2020
2.0.3 550 8/20/2020
2.0.2 576 8/19/2020
2.0.1 852 8/5/2020
2.0.0 719 7/30/2020

See https://github.com/simplify9/SW-Bus/releases for release notes and changelog.