RabbitMQAdvancedClient 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package RabbitMQAdvancedClient --version 1.0.0
                    
NuGet\Install-Package RabbitMQAdvancedClient -Version 1.0.0
                    
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="RabbitMQAdvancedClient" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RabbitMQAdvancedClient" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="RabbitMQAdvancedClient" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add RabbitMQAdvancedClient --version 1.0.0
                    
#r "nuget: RabbitMQAdvancedClient, 1.0.0"
                    
#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.
#:package RabbitMQAdvancedClient@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=RabbitMQAdvancedClient&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=RabbitMQAdvancedClient&version=1.0.0
                    
Install as a Cake Tool

RabbitMQAdvancedClient

����������� ����������-������ ��� ������ � RabbitMQ � .NET �����������. ������������ ������� � �������� API ��� ���������� � �������� �� ���������, � ���������� ��������� ������� �����������, ����������� � ���������� ��������������� ���������.

����������

�����������

  • ������� � ������� API ��� ������ � RabbitMQ
  • ������ ������������� (async/await)
  • �������������� ��������� ����������� ��� ������ ����������
  • ���������� ��������� ����������� � NLog
  • ������������/�������������� JSON ���������
  • ���������� ��������������� ��������� (Ack/Nack)
  • ������� ���������� � .NET Dependency Injection
  • �������������� ��������� (generic ����)

���������

��� ��������� ������ RabbitMQAdvancedClient ����������� NuGet Package Manager: dotnet add package RabbitMQAdvancedClient

���������

��������� �����������

��� ����������� � RabbitMQ ������� ���������� ��������� ��������� ����������� � ������� ������ RabbitMqOptions:

var options = new RabbitMqOptions
{
    Host = "localhost",     // ���� ������� RabbitMQ (�� ��������� "localhost")
    Port = 5672,            // ���� ������� RabbitMQ (�� ��������� 5672)
    Username = "guest",     // ��� ������������ (�� ��������� "guest")
    Password = "guest",     // ������ ������������ (�� ��������� "guest")
    RetryCount = 3,         // ���������� ������� ��������������� (�� ��������� 3)
    RetryDelay = 5          // �������� ����� ��������� � �������� (�� ��������� 5)
};

�������������

���������� � Dependency Injection

���������� ������������ ���������� � .NET Dependency Injection. �������� ��������� ��� � ����� ConfigureServices ������ Startup.cs ��� � ������������ ��������:

// ����������� � ������� �������� ���������
services.AddRabbitMq(options => 
{
    options.Host = "rabbitmq.example.com";
    options.Port = 5672;
    options.Username = "user";
    options.Password = "password";
    options.RetryCount = 5;
    options.RetryDelay = 10;
});

// ��� ����������� � �������������� �������������� ������������ �������
var rabbitOptions = new RabbitMqOptions
{
    Host = "rabbitmq.example.com",
    // ������ ���������...
};
services.AddRabbitMq(rabbitOptions);

����� ����������� �� ������ �������� IRabbitMqClient � ���� �������:

public class MyService
{
    private readonly IRabbitMqClient _rabbitMqClient;

    public MyService(IRabbitMqClient rabbitMqClient)
    {
        _rabbitMqClient = rabbitMqClient;
    }

    // ����������� _rabbitMqClient ��� ���������� � �������� �� ���������
}

���������� ���������

��� ���������� ��������� ����������� ����� PublishAsync<T>:

// ����������� ������ ���������
public class OrderCreatedMessage
{
    public int OrderId { get; set; }
    public decimal Amount { get; set; }
    public DateTime CreatedAt { get; set; }
}

// ���������� ���������
await _rabbitMqClient.PublishAsync("orders.created", new OrderCreatedMessage
{
    OrderId = 12345,
    Amount = 99.99m,
    CreatedAt = DateTime.UtcNow
});

�������� �� ���������

��� �������� �� ��������� ����������� ����� SubscribeAsync<T>:

// �������� � �������������� �������������� ���������
await _rabbitMqClient.SubscribeAsync<OrderCreatedMessage>(
    queueName: "orders.created",
    onMessageReceived: async (message, context) =>
    {
        Console.WriteLine($"������� ����� #{message.OrderId} �� ����� {message.Amount}");
        // ��������� ���������...
    },
    autoAck: true
);

// �������� � ������ �������������� ���������
await _rabbitMqClient.SubscribeAsync<OrderCreatedMessage>(
    queueName: "orders.created",
    onMessageReceived: async (message, context) =>
    {
        try
        {
            Console.WriteLine($"��������� ������ #{message.OrderId}");
            // ��������� ���������...
            
            // ������������� �������� ���������
            await context.AckAsync();
        }
        catch (Exception ex)
        {
            // ���������� ��������� � ��������� ����������� � �������
            await context.NackAsync(requeue: true);
        }
    },
    autoAck: false
);

���������� ��������������� ���������

��� ������� ���������� ��������������� ��������� ����������� ������ AckAsync()NackAsync() �� ������� MessageContext:

await _rabbitMqClient.SubscribeAsync<MyMessage>(
    queueName: "my.queue",
    onMessageReceived: async (message, context) =>
    {
        try
        {
            // �������� ���������
            await context.AckAsync();
        }
        catch (Exception)
        {
            // ��������� ���������, ��������� �����
            await context.NackAsync(requeue: true);
            
            // ��� ��������� � Dead Letter Queue
            // await context.NackAsync(requeue: false);
        }
    },
    autoAck: false
);

�������

������� ������

// ������� ������ ���������� � ��������

// 1. ��������� ��������
services.AddRabbitMq(options => 
{
    options.Host = "localhost";
    options.Username = "guest";
    options.Password = "guest";
});

// 2. ������������� � �������
public class NotificationService
{
    private readonly IRabbitMqClient _rabbitMqClient;

    public NotificationService(IRabbitMqClient rabbitMqClient)
    {
        _rabbitMqClient = rabbitMqClient;
    }

    public async Task SendNotificationAsync(string userId, string message)
    {
        await _rabbitMqClient.PublishAsync("notifications", new
        {
            UserId = userId,
            Message = message,
            Timestamp = DateTime.UtcNow
        });
    }

    public async Task StartNotificationProcessingAsync()
    {
        await _rabbitMqClient.SubscribeAsync<dynamic>(
            "notifications",
            async (notification, context) =>
            {
                Console.WriteLine($"�������� ����������� ������������ {notification.UserId}: {notification.Message}");
                // ������ �������� �����������...
            }
        );
    }
}

����������� ������

// ����� ������� ������ � ���������� ������ � ������ ��������������

// ����� ���������
public class PaymentProcessedMessage
{
    public string TransactionId { get; set; }
    public string CustomerId { get; set; }
    public decimal Amount { get; set; }
    public bool Success { get; set; }
    public string ErrorMessage { get; set; }
}

// ������ ��������� ��������
public class PaymentService
{
    private readonly IRabbitMqClient _rabbitMqClient;
    private readonly ILogger<PaymentService> _logger;

    public PaymentService(IRabbitMqClient rabbitMqClient, ILogger<PaymentService> logger)
    {
        _rabbitMqClient = rabbitMqClient;
        _logger = logger;
    }

    public async Task ProcessPaymentAsync(string customerId, decimal amount)
    {
        var transactionId = Guid.NewGuid().ToString();
        
        try
        {
            // ������ ��������� �������...
            bool paymentSuccessful = await ProcessPaymentLogic(customerId, amount);
            
            // ���������� ����������
            await _rabbitMqClient.PublishAsync("payments.processed", new PaymentProcessedMessage
            {
                TransactionId = transactionId,
                CustomerId = customerId,
                Amount = amount,
                Success = paymentSuccessful,
                ErrorMessage = paymentSuccessful ? null : "Insufficient funds"
            });
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "������ ��� ��������� �������");
            
            // ���������� ��������� �� ������
            await _rabbitMqClient.PublishAsync("payments.processed", new PaymentProcessedMessage
            {
                TransactionId = transactionId,
                CustomerId = customerId,
                Amount = amount,
                Success = false,
                ErrorMessage = ex.Message
            });
        }
    }

    public async Task StartPaymentNotificationServiceAsync()
    {
        await _rabbitMqClient.SubscribeAsync<PaymentProcessedMessage>(
            "payments.processed",
            async (payment, context) =>
            {
                try
                {
                    if (payment.Success)
                    {
                        _logger.LogInformation(
                            "�������� ������: ���������� {TransactionId}, ������ {CustomerId}, ����� {Amount}",
                            payment.TransactionId, payment.CustomerId, payment.Amount);
                        
                        // �������� ����������� ������� �� �������� �������
                        await SendSuccessNotification(payment);
                    }
                    else
                    {
                        _logger.LogWarning(
                            "��������� ������: ���������� {TransactionId}, ������ {CustomerId}, ������: {Error}",
                            payment.TransactionId, payment.CustomerId, payment.ErrorMessage);
                        
                        // �������� ����������� ������� � ��������� �������
                        await SendFailureNotification(payment);
                    }
                    
                    // ������������� ��������� ���������
                    await context.AckAsync();
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "������ ��� ��������� ����������� � �������");
                    
                    // ������� ��������� � ������� ��� ��������� ���������
                    await context.NackAsync(requeue: true);
                }
            },
            autoAck: false
        );
    }

    // ��������������� ������...
    private Task<bool> ProcessPaymentLogic(string customerId, decimal amount) => Task.FromResult(true);
    private Task SendSuccessNotification(PaymentProcessedMessage payment) => Task.CompletedTask;
    private Task SendFailureNotification(PaymentProcessedMessage payment) => Task.CompletedTask;
}

����������

  • .NET 9.0 ��� ����
  • RabbitMQ Server 3.8.0 ��� ����

��������

���� ������ ������������ ��� MIT License.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows 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 136 6/26/2025
1.0.2 145 6/1/2025
1.0.1 137 6/1/2025
1.0.0 140 6/1/2025