RabbitMQAdvancedClient 1.0.0
See the version list below for details.
dotnet add package RabbitMQAdvancedClient --version 1.0.0
NuGet\Install-Package RabbitMQAdvancedClient -Version 1.0.0
<PackageReference Include="RabbitMQAdvancedClient" Version="1.0.0" />
<PackageVersion Include="RabbitMQAdvancedClient" Version="1.0.0" />
<PackageReference Include="RabbitMQAdvancedClient" />
paket add RabbitMQAdvancedClient --version 1.0.0
#r "nuget: RabbitMQAdvancedClient, 1.0.0"
#:package RabbitMQAdvancedClient@1.0.0
#addin nuget:?package=RabbitMQAdvancedClient&version=1.0.0
#tool nuget:?package=RabbitMQAdvancedClient&version=1.0.0
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 | Versions 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. |
-
net9.0
- Microsoft.Extensions.DependencyInjection (>= 9.0.5)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.5)
- Microsoft.Extensions.Options (>= 9.0.5)
- NLog (>= 5.5.0)
- RabbitMQ.Client (>= 7.1.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.