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.
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.1.8 | obsolete | obsolete |
0.1.9 ~ 0.2.2 | 6.5.0-6.8.1 | 6.0, 7.0, 8.0 |
For details, see: NanoRabbit Wiki.
NanoRabbit is designed as a library depends on NAMING Connections, Producers and Consumers. So it's important to set a UNIQUE NAME for each Connections, Producers and Consumers.
For more, please visit the Examples.
Register a RabbitMQ Producer by calling RabbitHelper(RabbitConfiguration rabbitConfig, ILogger<RabbitHelper>? logger = null)
, and configure it.
using NanoRabbit;
using NanoRabbit.Connection;
var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddConsole();
});
var logger = loggerFactory.CreateLogger("RabbitHelper");
var rabbitHelper = new RabbitHelper(rabbitConfig: new RabbitConfiguration
{
HostName = "localhost",
Port = 5672,
VirtualHost = "/",
UserName = "admin",
Password = "admin",
Producers = new List<ProducerOptions> { new ProducerOptions {
ProducerName = "FooProducer",
ExchangeName = "amq.topic",
RoutingKey = "foo.key",
Type = ExchangeType.Topic
}
}
}, logger);
Register a RabbitMQ Consumer by calling RabbitHelper()
, and configure it.
using NanoRabbit;
using NanoRabbit.Connection;
var rabbitHelper = new RabbitHelper(rabbitConfig: new RabbitConfiguration
{
HostName = "localhost",
Port = 5672,
VirtualHost = "/",
UserName = "admin",
Password = "admin",
Consumers = new List<ConsumerOptions> { new ConsumerOptions {
ConsumerName= "FooConsumer",
QueueName = "foo-queue"
}
}
}, logger);
After registering a RabbitProducer
in the RabbitHelper
, you can simply publish a message by calling Publish<T>(string producerName, T message)
.
rabbitHelper.Publish<string>("FooProducer", "Hello from NanoRabbit");
Asynchronously publishing is also available:
await rabbitHelper.PublishAsync<string>("FooProducer", "Hello from NanoRabbit");
After registering a RabbitConsumer
in the RabbitConsumer
, you can simply consume a message by
calling AddConsumer(string consumerName, Action<string> onMessageReceived, int consumers = 1)
.
while (true)
{
rabbitHelper.AddConsumer("FooConsumer", message =>
{
Console.WriteLine(message);
});
}
Working on it.
NanoRabbit provides some functions to inject IRabbitHelper in a simple way.
var builder = Host.CreateApplicationBuilder(args);
// Configure the RabbitMQ Connection
builder.Services.AddRabbitHelper(builder =>
{
builder.SetHostName("localhost")
.SetPort(5672)
.SetVirtualHost("/")
.SetUserName("admin")
.SetPassword("admin")
.AddProducerOption(producer =>
{
producer.ProducerName = "FooProducer";
producer.ExchangeName = "amq.topic";
producer.RoutingKey = "foo.key";
producer.Type = ExchangeType.Topic;
});
});
using IHost host = builder.Build();
host.Run();
var builder = Host.CreateApplicationBuilder(args);
// Configure the RabbitMQ Connection
builder.Services.AddRabbitHelper(builder =>
{
builder.SetHostName("localhost")
.SetPort(5672)
.SetVirtualHost("/")
.SetUserName("admin")
.SetPassword("admin")
.AddConsumerOption(consumer =>
{
consumer.ConsumerName = "FooConsumer";
consumer.QueueName = "foo-queue";
})
.AddConsumerOption(consumer =>
{
consumer.ConsumerName = "BarConsumer";
consumer.QueueName = "bar-queue";
});
})
.AddRabbitConsumer<FooQueueHandler>("FooConsumer", consumers: 3)
.AddRabbitConsumer<BarQueueHandler>("BarConsumer", consumers: 2);
using IHost host = builder.Build();
host.Run();
More Dependency Injection 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
- DependencyInjection
- Logging
- Support .NET latest frameworks
- Forward messages
- ASP.NET support
- TLS support
- Re-trying of failed sends
- Basic RabbitMQ.Client functions extensions
- Visual Studio 2022
- RabbitMQ.Client
- Newtonsoft.Json
- Masstransit
- EasyNetQ
- Polly.Core
NanoRabbit is licensed under the MIT license.