Skip to content

velviagris/NanoRabbit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

480 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NanoRabbit logo

NuGet Nuget Downloads License

About

NanoRabbit, A Lightweight RabbitMQ .NET 3rd party library for .NET 8 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.3 6.5.0-6.8.1 6.0, 7.0, 8.0
0.2.4 6.5.0-6.8.1 8.0
0.3.0, 0.3.1 7.1.2 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 Connection, Producer and Consumer.

From RabbitMQ.Client 7.0.0 (NanoRabbit 0.3.0) on, synchronously functions are not suppoerted.

For more, please visit the Examples.

Setup Connections && Helpers && RabbitConsumers

Setup connections

Before using NanoRabbit, you should register IConnection and other configs by calling AddRabbitConnection( this IServiceCollection services, Action<RabbitConfigurationBuilder> builder)

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NanoRabbit;
using NanoRabbit.DependencyInjection;

var builder = Host.CreateApplicationBuilder(args);

// Configure the RabbitMQ Connection
builder.Services.AddRabbitConnection(x =>
    {
        x.SetHostName("localhost")
            .SetPort(5672)
            .SetVirtualHost("/")
            .SetUserName("admin")
            .SetPassword("admin")
            .SetConnectionName("FooConnection")
            .AddProducerOption(producer =>
            {
                producer.ProducerName = "FooProducer";
                producer.ExchangeName = "amq.topic";
                producer.RoutingKey = "foo.key";
                producer.Type = ExchangeType.Topic;
            })
            .AddConsumerOption(consumer =>
            {
                consumer.ConsumerName = "FooConsumer";
                consumer.QueueName = "foo-queue";
                consumer.ConsumerCount = 3;
                consumer.HandlerName = nameof(FooQueueHandler);
            })
            .AddConsumerOption(consumer =>
            {
                consumer.ConsumerName = "BarConsumer";
                consumer.QueueName = "bar-queue";
                consumer.ConsumerCount = 2;
                consumer.HandlerName = nameof(BarQueueHandler);
            });
    });

// todo: Add RabbitHelpers and RabbitCnsumers if neccessary.

using IHost host = builder.Build();

host.Run();

Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();

Add Consumers

Register a RabbitMQ Consumer by calling AddRabbitAsyncHandler<TAsyncHandler>() and AddRabbitConsumer(), you should declare HandlerName at the previous settings of RabbitConnection.

// Register the connection like before
builder.Services.AddRabbitConnection(x=>{});

// Register IAsyncHandler and RabbitConsumer
builder.Services.AddRabbitAsyncHandler<FooQueueHandler>()
    .AddRabbitAsyncHandler<BarQueueHandler>()
    .AddRabbitConsumer();

// Build and run the host
using IHost host = builder.Build();
host.Run();

When the host starts running, it will start some BackgroundService to start consuming.

Publish Messages

After registering RabbitConnection, you should register RabbitHelper, then you can simply publish a message by calling PublishAsync<T>(string producerName, T message).

// Register the connection like before
builder.Services.AddRabbitConnection(x=>{});

// Register a RabbitHelper
builder.Services.AddRabbitHelper();

// Build and run the host
using IHost host = builder.Build();
host.Run();

var rabbitMqHelper = host.Services.GetRequiredService<IRabbitHelper>();
await rabbitMqHelper.PublishAsync("FooProducer", "Hello, World!");

Consume Messages

After registering a RabbitConsumer, you should complete the TAsyncHandler, which is declared in AddRabbitConnection(x=>{}) and being registered by calling AddRabbitAsyncHandler<TAsyncHandler>().

For example:

public class FooQueueHandler : IAsyncMessageHandler
{
    public async Task HandleMessageAsync(byte[] messageBody, string? routingKey = null, string? correlationId = null)
    {
        var message = Encoding.UTF8.GetString(messageBody);
        Console.WriteLine($"[x] Received from foo-queue: {message}");
        await Task.Delay(1000);
        Console.WriteLine("[x] Done");
    }
}

Inherit IAsyncMessageHandler and implement Task HandleMessageAsync(byte[] messageBody, string? routingKey = null, string? correlationId = null).

Forward messages

Working on it.

More 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.

About

A Lightweight RabbitMQ .NET 3rd party library.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •