RabbitSharp 2.0.2
dotnet add package RabbitSharp --version 2.0.2
NuGet\Install-Package RabbitSharp -Version 2.0.2
<PackageReference Include="RabbitSharp" Version="2.0.2" />
<PackageVersion Include="RabbitSharp" Version="2.0.2" />
<PackageReference Include="RabbitSharp" />
paket add RabbitSharp --version 2.0.2
#r "nuget: RabbitSharp, 2.0.2"
#:package RabbitSharp@2.0.2
#addin nuget:?package=RabbitSharp&version=2.0.2
#tool nuget:?package=RabbitSharp&version=2.0.2
RabbitSharp
A high-performance, highly customizable RabbitMQ Pub/Sub library for .NET 6+ applications with built-in retry mechanisms and dead letter queue support.
Features
- ✅ Full Pub/Sub pattern implementation
- ✅ Compatible with .NET 6+
- ✅ Highly customizable configuration
- ✅ Built-in retry mechanisms with exponential backoff
- ✅ Dead letter queue support
- ✅ Network recovery and connection resilience
- ✅ Configurable heartbeat intervals
- ✅ Multiple host support for high availability
- ✅ Flexible exchange and routing configuration
- ✅ Convention-based naming with customizable overrides
- ✅ Pre-configured setups for different environments
Installation
dotnet add package RabbitSharp
Quick Start
- Configure your appsettings.json
{
"BrokerOptions": {
"TryConnectMaxRetries": 5,
"NetworkRecoveryIntervalInSeconds": 10,
"HeartbeatIntervalSeconds": 60,
"Hosts": [ "localhost" ],
"Username": "guest",
"Password": "guest",
"VirtualHost": "/"
}
}
- Register RabbitSharp in Program.cs
var brokerOptions = builder.Configuration.GetSection(nameof(BrokerOptions)).Get<BrokerOptions>() ?? new BrokerOptions();
builder.Services.AddRabbitSharp(options =>
{
options.Username = brokerOptions.Username;
options.Password = brokerOptions.Password;
options.VirtualHost = brokerOptions.VirtualHost;
options.Hosts = brokerOptions.Hosts;
});
With Resilience Configuration:
var brokerOptions = builder.Configuration.GetSection(nameof(BrokerOptions)).Get<BrokerOptions>() ?? new BrokerOptions();
builder.Services.AddRabbitSharp(options =>
{
options.Username = brokerOptions.Username;
options.Password = brokerOptions.Password;
options.VirtualHost = brokerOptions.VirtualHost;
options.Hosts = brokerOptions.Hosts;
}, busResilienceOptions =>
{
busResilienceOptions.InitialDeliveryRetryDelay = TimeSpan.FromSeconds(1);
busResilienceOptions.MaxDeliveryRetryDelay = TimeSpan.FromSeconds(10);
busResilienceOptions.MaxDeliveryRetryAttempts = 3;
});
- Create your message class
public class EventSimple : IMessage
{
public EventSimple(Guid correlationId)
{
CorrelationId = correlationId;
}
public EventSimple()
{
CorrelationId = Guid.NewGuid();
}
public Guid CorrelationId { get; init; }
}
- Publish messages
using var scope = serviceScopeFactory.CreateScope();
var bus = scope.ServiceProvider.GetRequiredService<IBus>();
var eventSimple = new EventSimple(Guid.NewGuid());
await bus.PublishAsync(eventSimple);
Publishing Messages
Basic Publishing The simplest way to publish a message. When using basic publishing, RabbitSharp automatically creates:
- Exchange: Topic type with name based on your message class name (e.g., "EventSimple")
- Routing Key: MessageName.# pattern (e.g., "EventSimple.#")
using var scope = serviceScopeFactory.CreateScope();
var bus = scope.ServiceProvider.GetRequiredService<IBus>();
var eventSimple = new EventSimple(Guid.NewGuid());
await bus.PublishAsync(eventSimple);
Advanced Publishing with Custom Options For more control over exchange configuration and routing:
using var scope = serviceScopeFactory.CreateScope();
var bus = scope.ServiceProvider.GetRequiredService<IBus>();
var options = new PublisherOptionsBuilder()
.WithExchange(exchange =>
{
exchange.WithName("publisher.event-simple");
exchange.WithType(ExchangeTypeEnum.Direct);
exchange.AsDurable();
exchange.WithArgument("alternate-exchange", "unrouted.exchange");
})
.WithRoutingKey("event.simple")
.Build();
var eventSimple = new EventSimple(Guid.NewGuid());
await bus.PublishAsync(options, eventSimple);
Message Requirements
public sealed record YourMessage : IMessage
{
public Guid CorrelationId { get; init; }
// Your message properties here
}
- Subscribing to Messages Basic Subscription (Convention-Based) The simplest way to consume messages. RabbitSharp will automatically connect to a Topic exchange named after your message class:
private void SetSubscribers()
{
bus.SubscribeAsync<EventSimple>("subscriber.event-simple", HandleEventAsync);
}
Advanced Subscription with Custom Configuration For full control over queue and exchange configuration:
private void SetSubscribers()
{
var options = new BusInfrastructureBuilder("subscriber.event-simple")
.WithExchange(exchange =>
{
exchange.WithName("publisher.event-simple");
exchange.WithType(ExchangeTypeEnum.Direct);
exchange.AsDurable();
exchange.WithArgument("alternate-exchange", "unrouted.exchange");
})
.WithMainQueue(queue =>
{
queue.AsDurable();
queue.WithMaxLength(10000);
queue.WithMaxPriority(10);
queue.WithOverflowBehavior(OverFlowStrategyEnum.RejectPublish);
queue.WithTTL(TimeSpan.FromDays(30));
})
.WithRoutingKey("event.simple")
.Build();
bus.SubscribeAsync<EventSimple>(options, HandleEventAsync);
}
Environment-Specific Presets
RabbitSharp provides pre-configured setups for different scenarios:
// For development environments
var devOptions = new BusInfrastructureBuilder("my-queue")
.ForDevelopment()
.Build();
// For high-throughput scenarios
var highThroughputOptions = new BusInfrastructureBuilder("my-queue")
.ForHighThroughput()
.Build();
Exchange Types
RabbitSharp supports the following exchange types:
- Topic: Pattern-based routing (default for convention-based publishing)
- Direct: Exact routing key matching
- Fanout: Broadcast to all bound queues
Best Practices
- RabbitSharp comes with sensible defaults following RabbitMQ best practices
- Even without manual configuration, retry queues and dead letter queues are automatically set up
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. 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 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 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. |
-
net6.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.4)
- Polly (>= 8.6.2)
- RabbitMQ.Client (>= 7.1.2)
-
net7.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 7.0.1)
- Polly (>= 8.6.2)
- RabbitMQ.Client (>= 7.1.2)
-
net8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)
- Polly (>= 8.6.2)
- RabbitMQ.Client (>= 7.1.2)
-
net9.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.0)
- Polly (>= 8.6.2)
- RabbitMQ.Client (>= 7.1.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Initial release with full Pub/Sub support, retry mechanisms, and dead letter queues.