TotallyTech.RabbitMQ 1.0.22
RabbitMQ Integration Package
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.
Configure RabbitMQ Settings: Define your RabbitMQ settings in the
appsettings.jsonfile:{ "RabbitMQ": { "HostName": "your_hostname", "UserName": "your_username", "Password": "your_password", "ExchangeName": "your_exchange_name", "QueueName": "your_queue_name", "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<PatientDto>>(); services.AddSingleton<IMessageConsumer<PatientDto>>(sp => { var factory = sp.GetRequiredService<RabbitMQFactory<PatientDto>>(); var queueName = "Laser"; var exchangeName = "LaserAppExchange"; return factory.CreateConsumer(queueName, exchangeName); }); services.AddScoped<IMessageProducer<PatientDto>>(sp => { var factory = sp.GetRequiredService<RabbitMQFactory<PatientDto>>(); var queueName = "Laser"; var exchangeName = "LaserAppExchange"; return factory.CreateProducer(queueName, exchangeName); }); services.AddSingleton<RabbitMQFactory<CreateService>>(); services.AddScoped<IMessageProducer<CreateService>>(sp => { var factory = sp.GetRequiredService<RabbitMQFactory<CreateService>>(); var queueName = "Warehouse"; var exchangeName = "WarehouseAppExchange"; return factory.CreateProducer(queueName, exchangeName); }); services.AddHostedService<ConsumerBackgroundService<PatientDto>>(); services.AddHostedService<RabbitMQHostedService>();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<PatientDto> _messageConsumer; private readonly IServiceScopeFactory _serviceScopeFactory; public RabbitMQHostedService(ILogger<RabbitMQHostedService> logger, IMessageConsumer<PatientDto> 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(PatientDto message) { _ = HandleMessageAsync(message); } private async Task HandleMessageAsync(PatientDto 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 laserPatientService = scope.ServiceProvider.GetRequiredService<ILaserPatient>(); var result = await laserPatientService.Add(message, CancellationToken.None); if (result.IsSuccess) { _logger.LogInformation("PatientDto added successfully."); } else { _logger.LogError("Failed to add PatientDto: {0}", result.Errors); } } } }
No packages depend on TotallyTech.RabbitMQ.
.NET 8.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)
| Version | Downloads | Last updated |
|---|---|---|
| 1.0.24 | 59 | 7/22/2024 |
| 1.0.23 | 11 | 7/21/2024 |
| 1.0.22 | 11 | 7/21/2024 |
| 1.0.21 | 49 | 7/18/2024 |
| 1.0.20 | 11 | 7/18/2024 |
| 1.0.19 | 15 | 7/18/2024 |
| 1.0.18 | 12 | 7/18/2024 |
| 1.0.17 | 10 | 7/17/2024 |
| 1.0.16 | 10 | 7/16/2024 |
| 1.0.15 | 12 | 7/16/2024 |
| 1.0.14 | 15 | 7/16/2024 |
| 1.0.13 | 12 | 7/16/2024 |
| 1.0.12 | 10 | 7/16/2024 |
| 1.0.11 | 16 | 7/14/2024 |
| 1.0.10 | 11 | 7/14/2024 |
| 1.0.9 | 9 | 7/14/2024 |
| 1.0.8 | 10 | 7/14/2024 |
| 1.0.7 | 10 | 7/14/2024 |
| 1.0.6 | 12 | 7/14/2024 |
| 1.0.5 | 10 | 7/14/2024 |
| 1.0.4 | 10 | 7/14/2024 |
| 1.0.3 | 10 | 7/14/2024 |
| 1.0.2 | 12 | 7/14/2024 |
| 1.0.1 | 11 | 7/14/2024 |
| 1.0.0 | 65 | 7/14/2024 |