EasyRabbitMQ.Net
1.0.1
dotnet add package EasyRabbitMQ.Net --version 1.0.1
NuGet\Install-Package EasyRabbitMQ.Net -Version 1.0.1
<PackageReference Include="EasyRabbitMQ.Net" Version="1.0.1" />
paket add EasyRabbitMQ.Net --version 1.0.1
#r "nuget: EasyRabbitMQ.Net, 1.0.1"
// Install EasyRabbitMQ.Net as a Cake Addin #addin nuget:?package=EasyRabbitMQ.Net&version=1.0.1 // Install EasyRabbitMQ.Net as a Cake Tool #tool nuget:?package=EasyRabbitMQ.Net&version=1.0.1
EasyRabbitMQ.Net
This package provides a comprehensive solution for integrating RabbitMQ messaging into your .NET application. It includes classes and interfaces for message producers, consumers, factories, and hosted services for background processing.
Features
- Message Producers
- Message Consumers
- RabbitMQ Factory
- Hosted Services for Background Processing
Installation
Add Package:
Add the package to your project using NuGet Package Manager or the .NET CLI:
dotnet add package EasyRabbitMQ.Net or https://www.nuget.org/packages/EasyRabbitMQ.Net
Configure RabbitMQ Settings:
Define your RabbitMQ settings in the appsettings.json file:
{ "RabbitMQ": { "HostName": "your_hostname", "UserName": "your_username", "Password": "your_password", "VirtualHost": "/", "Port": 5672, "Ssl": { "Enabled": false, "ServerName": "", "AcceptablePolicyErrors": "" } } }
Register Services: Register the necessary services in your Startup.cs or Program.cs file:
// RabbitMQ Config var rabbitMQSettingsSection = configuration.GetSection("RabbitMQ"); var rabbitMQSettings = rabbitMQSettingsSection.Get<RabbitMQSettings>(); services.Configure<RabbitMQSettings>(rabbitMQSettingsSection); services.AddSingleton<IConnectionFactory>(provider => { if (rabbitMQSettings == null) { throw new ApplicationException("RabbitMQ settings are missing or invalid."); } var factory = new ConnectionFactory { HostName = rabbitMQSettings.HostName, UserName = rabbitMQSettings.UserName, Password = rabbitMQSettings.Password, VirtualHost = rabbitMQSettings.VirtualHost, Port = rabbitMQSettings.Port, Ssl = new SslOption { Enabled = rabbitMQSettings.Ssl.Enabled, ServerName = rabbitMQSettings.Ssl.ServerName, AcceptablePolicyErrors = SslPolicyErrors.RemoteCertificateNameMismatch | SslPolicyErrors.RemoteCertificateChainErrors } }; Console.WriteLine(factory.ToString()); return factory; }); services.AddSingleton<RabbitMQFactory>(); services.AddSingleton<IMessageProducer>(sp => { var factory = sp.GetRequiredService<RabbitMQFactory>(); return factory.CreateProducer(); }); // Register the IMessageConsumer service services.AddSingleton<IMessageConsumer>(sp => { var factory = sp.GetRequiredService<RabbitMQFactory>(); return factory.CreateConsumer(); }); services.AddHostedService<RabbitMQHostedService>(sp => { var logger = sp.GetRequiredService<ILogger<RabbitMQHostedService>>(); var messageConsumer = sp.GetRequiredService<IMessageConsumer>(); var mediator = sp.GetRequiredService<IMediator>(); var serviceScopeFactory = sp.GetRequiredService<IServiceScopeFactory>(); var queueName = "test"; var exchangeName = "testAppExchange"; var routingKey = "test.add"; var exchangeType = TotallyTech.RabbitMQ.RabbitMQ.Enums.ExchangeType.Topic; return new RabbitMQHostedService(logger, messageConsumer, serviceScopeFactory, queueName, exchangeName, routingKey, exchangeType, mediator); });
Implement Message Handling: Create a hosted service to handle incoming messages and perform the necessary business logic:
public class RabbitMQHostedService : IHostedService { private readonly ILogger<RabbitMQHostedService> _logger; private readonly IMessageConsumer<testDto> _messageConsumer; private readonly IServiceScopeFactory _serviceScopeFactory; public RabbitMQHostedService(ILogger<RabbitMQHostedService> logger, IMessageConsumer<testDto> messageConsumer, IServiceScopeFactory serviceScopeFactory) { _logger = logger; _messageConsumer = messageConsumer; _serviceScopeFactory = serviceScopeFactory; } public Task StartAsync(CancellationToken cancellationToken) { _logger.LogInformation("RabbitMQ Hosted Service is starting."); // Set up the message handler _messageConsumer.OnMessageReceived = MessageReceivedHandler; return Task.CompletedTask; } public Task StopAsync(CancellationToken cancellationToken) { _logger.LogInformation("RabbitMQ Hosted Service is stopping."); return Task.CompletedTask; } private void MessageReceivedHandler(testDto message) { _ = HandleMessageAsync(message); } private async Task HandleMessageAsync(testDto message) { _logger.LogInformation("MessageReceivedHandler invoked in RabbitMQHostedService."); _logger.LogInformation($"Received message: {message}"); _logger.LogInformation(message.Name); // Create a scope to resolve scoped services using (var scope = _serviceScopeFactory.CreateScope()) { var testService = scope.ServiceProvider.GetRequiredService<Itest>(); var result = await testService.Add(message, CancellationToken.None); if (result.IsSuccess) { _logger.LogInformation("testDto added successfully."); } else { _logger.LogError("Failed to add testDto: {0}", result.Errors); } } } }
implement send to rabbitmq method
inject
private readonly IMessageProducer _messageProducer; // Declare MessageProducer public test( IMessageProducer messageProducer) { _messageProducer = messageProducer; } private void SendMessageToRabbitMQ(testRequestDto command, string queueName, string exchangeName, string routingKey, ExchangeType exchangeType) { try { _messageProducer.SendMessage(command, queueName, exchangeName, routingKey, exchangeType); } catch (Exception ex) { // Handle exception Console.WriteLine("Error sending message to RabbitMQ" + ex.Message.ToString()); } }
New Features
We have added two sample projects to demonstrate the usage of this package:
- Producer Sample Project: Demonstrates how to send messages using
EasyRabbitMQ.Net
. - Consumer Sample Project: Demonstrates how to consume messages using
EasyRabbitMQ.Net
with enhanced error handling.
These projects provide a clear example of how to set up and use the EasyRabbitMQ.Net
package for message production and consumption. You can find the complete implementation in the ProducerApp
and ConsumerApp
directories.
Enhanced Error Handling
The package now includes enhanced error handling for the consumer to provide a better user experience. Detailed error information is logged, and optional requeueing can be performed for failed messages.
For more information, please visit the GitHub repository.
This README.md
provides clear and comprehensive instructions for installing and using your EasyRabbitMQ.Net
package.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. |
-
net8.0
- Microsoft.Extensions.Configuration (>= 8.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 8.0.2)
- Microsoft.Extensions.Hosting (>= 8.0.0)
- Microsoft.Extensions.Logging (>= 8.0.0)
- Newtonsoft.Json (>= 13.0.3)
- RabbitMQ.Client (>= 6.8.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
- Enhanced error handling on the consumer side.