NanoRabbit, A Lightweight RabbitMQ .NET 3rd party library for .NET 6 and up, which makes a simple way to manage * Multiple* connections, producers, consumers, and easy to use.
NanoRabbit is under development! Please note that some APIs may change their names or usage!
Branch | Building Status |
---|---|
master | |
dev |
- Customize the name of producers, consumers.
- Dependency injection available.
- Multiple connections, producers, and consumers can be created.
You can get NanoRabbit by grabbing the latest NuGet package.
See Wiki for more details.
NanoRabbit | RabbitMQ.Client | .NET |
---|---|---|
0.0.1, 0.0.2, 0.0.3, 0.0.4, 0.0.5, 0.0.6 | 6.5.0 | 6.0 |
0.0.7 | 6.5.0 | 6.0, 7.0, 8.0 |
0.0.8, 0.0.9 | 6.7.0 | 6.0, 7.0, 8.0 |
0.1.0, 0.1.1, 0.1.2, 0.1.3, 0.1.4, 0.1.5, 0.1.6 | 6.8.1 | 6.0, 7.0, 8.0 |
0.1.7 | 6.5.0-6.8.1 | 6.0, 7.0, 8.0 |
The NanoRabbit Document is at NanoRabbit Wiki.
NanoRabbit is designed as a library depends on NAMING Producers, Consumers. So it's important to set a UNIQUE NAME for each Producers, Consumers.
For more, please visit the Examples.
Register a RabbitMQ Producer by calling RabbitProducer()
, and configure it.
var producer = new RabbitProducer(new[]
{
new ProducerOptions
{
ProducerName = "FooFirstQueueProducer",
HostName = "localhost",
Port = 5672,
UserName = "admin",
Password = "admin",
VirtualHost = "FooHost",
ExchangeName = "amq.topic",
RoutingKey = "FooFirstKey",
Type = ExchangeType.Topic,
Durable = true,
AutoDelete = false,
AutomaticRecoveryEnabled = true
}
});
Register a RabbitMQ Consumer by calling RabbitConsumer()
, and configure it.
var consumer = new RabbitConsumer(new[]
{
new ConsumerOptions
{
ConsumerName = "FooSecondQueueConsumer",
HostName = "localhost",
Port = 5672,
UserName = "admin",
Password = "admin",
VirtualHost = "FooHost",
QueueName = "FooSecondQueue",
AutomaticRecoveryEnabled = true
}
});
After creating the RabbitProducer
, you can simply publish a message by calling Publish<T>()
.
producer.Publish<string>("FooFirstQueueProducer", "Hello");
After creating the RabbitConsumer
, you can simply consume a message by
inheriting RabbitSubscriber
.
public class ConsumeService : RabbitSubscriber
{
public ConsumeService(IRabbitConsumer consumer, string consumerName, ILogger<RabbitSubscriber>? logger = null) : base(consumer, consumerName, logger)
{
// ...
}
protected override bool HandleMessage(string message)
{
// ...
return true;
}
}
var consumeService = new ConsumeService(consumer, null, "FooSecondQueueConsumer");
consumeService.StartAsync(CancellationToken.None);
Working on it.
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddRabbitProducer(options =>
{
options.AddProducer(new ProducerOptions
{
ProducerName = "FooFirstQueueProducer",
HostName = "localhost",
Port = 5672,
UserName = "admin",
Password = "admin",
VirtualHost = "FooHost",
ExchangeName = "amq.topic",
RoutingKey = "FooFirstKey",
Type = ExchangeType.Topic,
Durable = true,
AutoDelete = false,
Arguments = null,
AutomaticRecoveryEnabled = true
});
options.AddProducer(new ProducerOptions
{
ProducerName = "BarFirstQueueProducer",
HostName = "localhost",
Port = 5672,
UserName = "admin",
Password = "admin",
VirtualHost = "BarHost",
ExchangeName = "amq.direct",
RoutingKey = "BarFirstKey",
Type = ExchangeType.Direct,
Durable = true,
AutoDelete = false,
Arguments = null,
AutomaticRecoveryEnabled = true
});
});
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddRabbitConsumer(options =>
{
options.AddConsumer(new ConsumerOptions
{
ConsumerName = "FooFirstQueueConsumer",
HostName = "localhost",
Port = 5672,
UserName = "admin",
Password = "admin",
VirtualHost = "FooHost",
QueueName = "FooFirstQueue",
AutomaticRecoveryEnabled = true
});
options.AddConsumer(new ConsumerOptions
{
ConsumerName = "BarFirstQueueConsumer",
HostName = "localhost",
Port = 5672,
UserName = "admin",
Password = "admin",
VirtualHost = "BarHost",
QueueName = "BarFirstQueue", AutomaticRecoveryEnabled = true
});
});
After adding RabbitConsumer
and inheriting RabbitSubscriber
, you should register the BackgroundService
by AddRabbitSubscriber
:
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddRabbitSubscriber<ConsumeService>("FooSecondQueueConsumer");
From 0.1.6 on, setting Consumer Count
in AddRabbitSubscriber() is available.
Then, you can use RabbitProducer and RabbitConsumer at anywhere.
For example:
public class ConsumeService : RabbitSubscriber
{
private readonly IRabbitProducer _producer;
public ConsumeService(IRabbitConsumer consumer, ILogger<RabbitSubscriber>? logger, string consumerName, IRabbitProducer producer) : base(consumer, consumerName, logger)
{
_producer = producer;
}
protected override bool HandleMessage(string message)
{
_producer.Publish("FooSecondQueueProducer", message);
return true;
}
}
More DI Usage at Wiki.
- Fork this repository.
- Create a new branch in you current repos from the dev branch.
- Push commits and create a Pull Request (PR) to NanoRabbit.
- Basic Consume & Publish support
- DependencyInjection support
- Logging support
- Forward messages
- ASP.NET support
- Exchange Configurations
- .NET 7 support
- .NET 8 support
- Caching of failed sends
- Visual Studio 2022
- RabbitMQ.Client
- Newtonsoft.Json
- Masstransit
- EasyNetQ
NanoRabbit is licensed under the MIT license.