RabbitMQClientLibrary 1.0.3.3

dotnet add package RabbitMQClientLibrary --version 1.0.3.3                
NuGet\Install-Package RabbitMQClientLibrary -Version 1.0.3.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RabbitMQClientLibrary" Version="1.0.3.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RabbitMQClientLibrary --version 1.0.3.3                
#r "nuget: RabbitMQClientLibrary, 1.0.3.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install RabbitMQClientLibrary as a Cake Addin
#addin nuget:?package=RabbitMQClientLibrary&version=1.0.3.3

// Install RabbitMQClientLibrary as a Cake Tool
#tool nuget:?package=RabbitMQClientLibrary&version=1.0.3.3                

RabbitMq Connector Middleware

Overview

The RabbitMq Connector Middleware is a powerful tool for integrating RabbitMQ messaging into your application. It provides a simple and efficient way to send and receive messages using RabbitMQ as the messaging broker.

Features

  • Easy setup and configuration
  • Publish and consume messages with ease
  • Support for message acknowledgements and rejections
  • Error handling and retry mechanisms
  • Extensible and customizable

Installation

To install the RabbitMq Connector Middleware, you can use NuGet:

Usage

Create a config file(appSettings.json) or create a new instance of RabbitMqConfiguration class.

{
  "RabbitMqConfiguration": {
    "Host": "127.0.0.1",
    "Port": 5672,
    "Username": "rabbitmq",
    "Password": "rabbitmq",
    "ConsumerQueueName": "TestAppExchange",
    "ConsumerExchangeName": "TestAppQueue",
    "PublisherQueueName": "PublishAppExchange",
    "PublisherExchangeName": "PublishAppQueue",
    "ClusterUrls": ["127.0.0.1"],
    "Heartbeat": 15,
    "RetryCount": 5,
    "ConnectionName": "TestApp"
  }
}
var rabbitMqConfig = new RabbitMqConfiguration
{
    Host = "127.0.0.1",
    Port = 5672,
    Username = "guest",
    Password = "guest",
    Heartbeat = 30,
    ClusterUrls = ["127.0.0.1"],
    ConnectionName = "TestApp",
    ConsumerExchangeName = "TestAppExchange",
    ConsumerQueueName = "TestAppQueue",
    PublisherExchangeName = "PublishAppExchange",
    PublisherQueueName = "PublishAppQueue",
    RetryCount = 3
};

Then create a consumer class or publisher class based on RabbitMqPublisherBase<T> T is model class of message exchange.

Create a publisher class

public class ServerMessageQueuePublisher : RabbitMqPublisherBase<UserMessage>
{
    public ServerMessageQueuePublisher(RabbitMqConfiguration rabbitMqConfigOptions,
            ILogger<RabbitMqPublisherBase<UserMessage>> logger, IRabbitMqService rabbitMQPersistentConnection)
        : base(logger, rabbitMQPersistentConnection, rabbitMqConfigOptions.PublisherQueueName,
            rabbitMqConfigOptions.PublisherExchangeName)
    {
    }
}

///message exchange class
public class UserMessage
{
    public string? UserId { get; set; }
    public string? UserName { get; set; }
    public string? Message { get; set; }
}

Create a consumer class

public class ServerMessageQueueConsumer : RabbitMqSubscriberBase<UserMessage>
{
    public ServerMessageQueueConsumer(RabbitMqConfiguration rabbitMqConfigOptions,
            ILogger<RabbitMqSubscriberBase<UserMessage>> logger, IRabbitMqService rabbitMQPersistentConnection)
        : base(logger, rabbitMQPersistentConnection, rabbitMqConfigOptions.ConsumerQueueName,
            rabbitMqConfigOptions.ConsumerExchangeName)
    {
    }

    public async override Task HandleMessage(UserMessage message)
    {
        Console.WriteLine($"Message Received: {message.Message}");
    }
}

Startup.cs

Add methods below into the Startup.cs.

    private void AddRabbitMq(IServiceCollection services, IConfiguration configuration)
    {
        var rabbitMqConfig = configuration.GetSection("RabbitMqConfiguration").Get<RabbitMqConfiguration>();
        services.Configure<RabbitMqConfiguration>(configuration.GetSection("RabbitMqConfiguration"));
        services.AddSingleton<RabbitMqConfiguration>(rabbitMqConfig);
        services.AddSingleton<IRabbitMqService, RabbitMqService>(sp =>
        {
            var logger = sp.GetRequiredService<ILogger<RabbitMqService>>();
            var config = sp.GetRequiredService<IOptions<RabbitMqConfiguration>>().Value;
            return new RabbitMqService(config, logger);
        });
    }

    private void AddRabbitMqPublisher(IServiceCollection services)
    {
        services.AddScoped<ServerMessageQueuePublisher>();
    }
    private void AddRabbitMqConsumer(IServiceCollection services)
    {
        services.AddHostedService<ServerMessageQueueConsumer>();
    }

ConfigureServices :

    AddRabbitMq(context.Services, configuration);
    AddRabbitMqPublisher(context.Services);
    AddRabbitMqConsumer(context.Services);

Then Use Middleware into the app builder

app.UseRabbitMqConnector();

Usage :

  • Consumer : In ServerMessageQueueConsumer class has coming with an abstract method named : HandleMessage. you can use this method to process messages from queue.
    public async override Task HandleMessage(UserMessage message)
    {
        Console.WriteLine($"Message Received: {message.Message}");
    }
  • Publisher : Use DI for use the publisher class.
private readonly ServerMessageQueuePublisher _serverMessageQueuePublisher;

public Dashboard(ServerMessageQueuePublisher serverMessageQueuePublisher)
{
    _serverMessageQueuePublisher = serverMessageQueuePublisher;
}

///you can use this part any of your code.
var message = new UserMessage();
message = new UserMessage
{
    UserId = "1", // "1" is the admin user id, it should be fetched from the current user's id
    UserName = "admin",
    Message = "Dashboard page is visited"
};

await _serverMessageQueuePublisher.SendAsync(message);

Thank you.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.3.3 112 3/30/2024
1.0.3.2 98 3/30/2024