RabbitWire 1.0.3
dotnet add package RabbitWire --version 1.0.3
NuGet\Install-Package RabbitWire -Version 1.0.3
<PackageReference Include="RabbitWire" Version="1.0.3" />
<PackageVersion Include="RabbitWire" Version="1.0.3" />
<PackageReference Include="RabbitWire" />
paket add RabbitWire --version 1.0.3
#r "nuget: RabbitWire, 1.0.3"
#:package RabbitWire@1.0.3
#addin nuget:?package=RabbitWire&version=1.0.3
#tool nuget:?package=RabbitWire&version=1.0.3
π RabbitWire
RabbitWire is a lightweight .NET library that simplifies RabbitMQ integration using MassTransit. It provides a convention-based setup for producers and consumers, automatic queue naming with environment prefixes, built-in retry policies, and a clean service abstraction β so you can focus on your business logic instead of messaging plumbing.
β¨ Features
- One-line setup β Register RabbitMQ with a single
AddRabbitWire()call. - Convention-based queue naming β Queue names are automatically derived from consumer class names with a configurable prefix (e.g.
dev.productcreated.event.v1). - Environment-aware prefixes β Use prefixes like
dev,test,stage,prodto isolate queues per environment. - Built-in retry & error handling β Exponential backoff and interval-based retries are configured out of the box.
- SSL/TLS support β Optionally enable TLS 1.2 for secure connections.
- Multiple consumers β Pass as many consumer types as you need in a single registration call.
- Clean send abstraction β Use
IRabbitWireServiceto send messages to any queue URI.
π¦ Installation
dotnet add package RabbitWire
Or via the NuGet Package Manager:
Install-Package RabbitWire
π Quick Start
1. Add Configuration
Add the following section to your appsettings.json:
{
"RabbitMqTransportOptions": {
"Host": "localhost",
"VHost": "/",
"User": "guest",
"Pass": "guest",
"Port": 5672,
"UseSsl": false,
"Prefix": "dev"
}
}
| Property | Description |
|---|---|
Host |
RabbitMQ server hostname. |
VHost |
Virtual host (default /). |
User |
RabbitMQ username. |
Pass |
RabbitMQ password. |
Port |
RabbitMQ port (default 5672). |
UseSsl |
Set to true to enable TLS 1.2. |
Prefix |
Environment prefix for queue names (e.g. dev, test, prod). |
2. Register RabbitWire in Program.cs
builder.Services.AddRabbitWire(
configuration,
typeof(ProductCreatedConsumer),
typeof(ProductUpdatedConsumer)
);
AddRabbitWire accepts multiple consumer types via params. Each consumer is automatically wired to a receive endpoint with a queue name derived from its class name and the configured prefix.
π Usage
Sending Messages
RabbitWire provides IRabbitWireService for sending messages to RabbitMQ queues.
Define a Queue URI Provider (Recommended)
Create a small abstraction that maps your domain events to queue URIs. This keeps queue addresses centralized and easy to maintain.
IQueueUriProvider.cs
public interface IQueueUriProvider
{
Uri ProductCreatedMessage { get; }
Uri ProductUpdatedMessage { get; }
Uri ProductDeletedMessage { get; }
}
QueueUriProvider.cs
using MessageBroker.RabbitMq;
using Microsoft.Extensions.Options;
public class QueueUriProvider : IQueueUriProvider
{
private readonly RabbitWireConfig _rabbitMqOptions;
public QueueUriProvider(IOptions<RabbitWireConfig> options)
{
_rabbitMqOptions = options.Value;
}
public Uri ProductCreatedMessage =>
new Uri($"queue:{_rabbitMqOptions.Prefix}.mystore.productcreated.event.v1");
public Uri ProductUpdatedMessage =>
new Uri($"queue:{_rabbitMqOptions.Prefix}.mystore.productupdated.event.v1");
public Uri ProductDeletedMessage =>
new Uri($"queue:{_rabbitMqOptions.Prefix}.mystore.productdeleted.event.v1");
}
Register it in Program.cs:
builder.Services.AddScoped<IQueueUriProvider, QueueUriProvider>();
Send a Message from a Handler
public class ProductCreateHandler : IRequestHandler<CreateProductCommand, bool>
{
private readonly IRabbitWireService _bus;
private readonly IQueueUriProvider _queueUri;
public ProductCreateHandler(IRabbitWireService bus, IQueueUriProvider queueUri)
{
_bus = bus;
_queueUri = queueUri;
}
public async Task<bool> Handle(CreateProductCommand request, CancellationToken cancellationToken)
{
var productCreatedEvent = new ProductCreatedEvent
{
Id = 1,
Name = "Xbox",
Price = 480.00m
};
await _bus.Send(productCreatedEvent, _queueUri.ProductCreatedMessage, cancellationToken);
return true;
}
}
Consuming Messages
Create a consumer class that implements MassTransit's IConsumer<T>:
public class ProductCreatedConsumer : IConsumer<ProductCreatedEvent>
{
private readonly IMediator _mediator;
public ProductCreatedConsumer(IMediator mediator)
{
_mediator = mediator;
}
public async Task Consume(ConsumeContext<ProductCreatedEvent> context)
{
var product = new ProductAddedEvent
{
ProductId = context.Message.Id,
Name = context.Message.Name,
Price = context.Message.Price
};
await _mediator.Send(product);
}
}
Then handle the domain event however you like β persist to a database, trigger notifications, etc.:
public class ProductAddedEventHandler : IRequestHandler<ProductAddedEvent, bool>
{
private readonly MyDbContext _context;
private readonly IMapper _mapper;
public ProductAddedEventHandler(MyDbContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}
public async Task<bool> Handle(ProductAddedEvent request, CancellationToken cancellationToken)
{
var product = _mapper.Map<Products>(request);
_context.Products.Add(product);
await _context.SaveChangesAsync(cancellationToken);
return true;
}
}
π§ How It Works
Queue Naming Convention
RabbitWire automatically derives queue names from consumer class names using the following convention:
{Prefix}.{consumerName without "Consumer" suffix}.event.v1
Examples:
| Consumer Class | Prefix | Generated Queue Name |
|---|---|---|
ProductCreatedConsumer |
dev |
dev.productcreated.event.v1 |
ProductUpdatedConsumer |
prod |
prod.productupdated.event.v1 |
OrderShippedConsumer |
test |
test.ordershipped.event.v1 |
Built-in Retry Policy
Each consumer endpoint is configured with:
- Interval retry: 3 attempts with a 3-second delay between each.
- Exponential backoff: 3 attempts starting at 5 seconds, up to 1 hour max, with a 10-second increment.
- Error logging via Serilog on each retry attempt.
ConsumerCanceledExceptionis explicitly ignored by the retry policy.
Endpoint Defaults
| Setting | Default Value |
|---|---|
PrefetchCount |
5 |
ConfigureConsumeTopology |
false |
AutoDelete |
false |
ExchangeType |
Fanout |
ποΈ Architecture Overview
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Your Application β
β β
β ββββββββββββββββ ββββββββββββββββββββ β
β β Handler βββββΆβ IRabbitWireServiceβββββ Send ββββΆ π β
β ββββββββββββββββ ββββββββββββββββββββ RabbitMQβ
β β
β ββββββββββββββββββββββββββββ β
β β ProductCreatedConsumer ββββ Consume ββββββββββββ π β
β β ProductUpdatedConsumer β RabbitMQβ
β ββββββββββββββββββββββββββββ β
β β
β Program.cs β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β builder.Services.AddRabbitWire(config, consumers)β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
π API Reference
AddRabbitWire
public static IServiceCollection AddRabbitWire(
this IServiceCollection services,
IConfiguration configuration,
params Type[] consumerAssemblies)
Registers MassTransit with RabbitMQ transport and configures all provided consumer types. Reads connection settings from the RabbitMqTransportOptions configuration section.
IRabbitWireService
| Method | Description |
|---|---|
Send<T>(T message, Uri queueName, CancellationToken ct) |
Sends a message to a specific queue URI. |
Send<T>(T message, CancellationToken ct) |
Sends a message using MassTransit endpoint conventions. |
| Property | Description |
|---|---|
InstanceId |
Unique identifier for this service instance. |
Bus |
The underlying MassTransit IBusControl. |
RabbitWireConfig
Extends MassTransit's RabbitMqTransportOptions with:
| Property | Type | Description |
|---|---|---|
Prefix |
string? |
Environment prefix appended to queue names. |
π SSL / TLS
To enable secure connections, set UseSsl to true in your configuration:
{
"RabbitMqTransportOptions": {
"Host": "rabbitmq.example.com",
"UseSsl": true
}
}
RabbitWire will use TLS 1.2 for the connection.
π Requirements
- .NET 10 or later
- RabbitMQ server (local or remote)
- MassTransit 9.x (included as a dependency)
π€ Contributing
Contributions are welcome! Feel free to open issues or submit pull requests on GitHub.
- Fork the repository
- Create a feature branch (
git checkout -b feature/my-feature) - Commit your changes (
git commit -m 'Add my feature') - Push to the branch (
git push origin feature/my-feature) - Open a Pull Request
π License
This project is licensed under the terms of the license included in the repository. See the LICENSE file for details.
Made with β€οΈ by Casoft
| Product | Versions 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. |
-
net10.0
- MassTransit (>= 8.5.8)
- MassTransit.AspNetCore (>= 7.3.1)
- MassTransit.RabbitMQ (>= 8.5.8)
- Microsoft.Extensions.Configuration.Abstractions (>= 10.0.3)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.3)
- Microsoft.Extensions.Hosting (>= 10.0.3)
- Microsoft.VisualStudio.Azure.Containers.Tools.Targets (>= 1.23.0)
- Serilog (>= 4.3.1)
- Serilog.Sinks.Console (>= 6.1.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.