Skip to content

ifgris/NanoRabbit

Repository files navigation

NanoRabbit logo

NuGet Nuget Downloads License codebeat badge

About

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.

Building

Branch Building Status
master build
dev build

Features

  • Customize the name of producers, consumers.
  • Dependency injection available.
  • Multiple connections, producers, and consumers can be created.

Installation

You can get NanoRabbit by grabbing the latest NuGet package.

See Wiki for more details.

Version

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

Document

For details, see: NanoRabbit Wiki.

QuickStart

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.

Setup RabbitProducers && RabbitConsumers

RabbitProducer

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);

RabbitConsumer

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);

Simple Publish Messages

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");

Simple Consume Messages

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);
    });
}

Forward messages

Working on it.

DependencyInjection

NanoRabbit provides some functions to inject IRabbitHelper in a simple way.

AddRabbitProducer

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();

AddRabbitConsumer

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.

Contributing

  1. Fork this repository.
  2. Create a new branch in you current repos from the dev branch.
  3. Push commits and create a Pull Request (PR) to NanoRabbit.

Todo

  • 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

Thanks

License

NanoRabbit is licensed under the MIT license.