Refactor RabbitMQ functionality to new project
Moved RabbitMQ-related functionality from the `DigitalData.MessagingService.Infrastructure` project to a new dedicated project/namespace `DigitalData.MessagingService.RabbitMQ`. - Updated `DependencyInjection.cs` to use the new namespace. - Added a project reference to `RabbitMQ.csproj` in the `Infrastructure.csproj` file. - Removed `RabbitMqConfiguration.cs` and `RabbitMqConnectionFactory.cs` from the `Infrastructure` project. - Updated namespaces in `OutgoingEmailConsumer.cs`, `OutgoingEmailPublisher.cs`, and `AsyncInitWorker.cs` to use `DigitalData.MessagingService.RabbitMQ`. This refactor improves modularity, maintainability, and separation of concerns by isolating RabbitMQ functionality in its own project.
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
using DigitalData.MessagingService.Application.Common.Interfaces;
|
||||
using DigitalData.MessagingService.Infrastructure.Messaging;
|
||||
using DigitalData.MessagingService.Infrastructure.Queue;
|
||||
using DigitalData.MessagingService.Infrastructure.Services;
|
||||
using DigitalData.MessagingService.Infrastructure.Services.Background;
|
||||
using DigitalData.MessagingService.RabbitMQ;
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\DigitalData.MessagingService.Domain\DigitalData.MessagingService.Domain.csproj" />
|
||||
<ProjectReference Include="..\DigitalData.MessagingService.Application\DigitalData.MessagingService.Application.csproj" />
|
||||
<ProjectReference Include="..\DigitalData.MessagingService.RabbitMQ\DigitalData.MessagingService.RabbitMQ.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@@ -33,4 +34,8 @@
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Messaging\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
namespace DigitalData.MessagingService.Infrastructure.Messaging;
|
||||
|
||||
/// <summary>
|
||||
/// Configuration for RabbitMQ connection
|
||||
/// </summary>
|
||||
public class RabbitMqConfiguration
|
||||
{
|
||||
/// <summary>
|
||||
/// Configuration section name in appsettings.json
|
||||
/// </summary>
|
||||
public const string SectionName = "RabbitMQ";
|
||||
|
||||
/// <summary>
|
||||
/// RabbitMQ server hostname
|
||||
/// </summary>
|
||||
public string HostName { get; set; } = "localhost";
|
||||
|
||||
/// <summary>
|
||||
/// RabbitMQ AMQP port (default: 5672)
|
||||
/// </summary>
|
||||
public int Port { get; set; } = 5672;
|
||||
|
||||
/// <summary>
|
||||
/// RabbitMQ username
|
||||
/// </summary>
|
||||
public string UserName { get; set; } = "guest";
|
||||
|
||||
/// <summary>
|
||||
/// RabbitMQ password
|
||||
/// </summary>
|
||||
public string Password { get; set; } = "guest";
|
||||
|
||||
/// <summary>
|
||||
/// Virtual host (default: /)
|
||||
/// </summary>
|
||||
public string VirtualHost { get; set; } = "/";
|
||||
|
||||
/// <summary>
|
||||
/// Enable automatic recovery on connection failure
|
||||
/// </summary>
|
||||
public bool AutomaticRecoveryEnabled { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Network recovery interval in seconds
|
||||
/// </summary>
|
||||
public int NetworkRecoveryIntervalSeconds { get; set; } = 10;
|
||||
|
||||
public string QueueName { get; set; } = null!;
|
||||
public string ExchangeName { get; set; } = null!;
|
||||
public string RoutingKey { get; set; } = null!;
|
||||
public string DlqQueueName { get; set; } = null!;
|
||||
public string DlqExchangeName { get; set; } = null!;
|
||||
public string DlqRoutingKey { get; set; } = null!;
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using DigitalData.MessagingService.Application.Common.Interfaces;
|
||||
using DigitalData.MessagingService.Infrastructure.Messaging;
|
||||
using DigitalData.MessagingService.RabbitMQ;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using RabbitMQ.Client;
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using DigitalData.MessagingService.Application.Common.Interfaces;
|
||||
using DigitalData.MessagingService.Infrastructure.Messaging;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using RabbitMQ.Client;
|
||||
using RabbitMQ.Client.Events;
|
||||
using DigitalData.MessagingService.Application.Common.Events;
|
||||
using DevExpress.CodeParser;
|
||||
using DigitalData.MessagingService.RabbitMQ;
|
||||
|
||||
namespace DigitalData.MessagingService.Infrastructure.Queue;
|
||||
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
using DigitalData.MessagingService.Infrastructure.Messaging;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using RabbitMQ.Client;
|
||||
|
||||
namespace DigitalData.MessagingService.Infrastructure.Queue;
|
||||
|
||||
public sealed class RabbitMqConnectionFactory : IAsyncDisposable
|
||||
{
|
||||
private readonly RabbitMqConfiguration _config;
|
||||
|
||||
private readonly ILogger<RabbitMqConnectionFactory>? _logger;
|
||||
|
||||
private readonly Lazy<Task<IConnection>> _lazyConnectionProvider;
|
||||
|
||||
private IConnection? _connection = null;
|
||||
|
||||
private CancellationToken? _cancellationToken;
|
||||
|
||||
public CancellationToken CancellationToken => _cancellationToken
|
||||
?? throw new InvalidOperationException("RabbitMqConnectionFactory is not initialized. Call InitAsync() before using this method.");
|
||||
|
||||
public Task<IConnection> GetConnectionAsync()
|
||||
{
|
||||
if (_cancellationToken is not null)
|
||||
return _lazyConnectionProvider.Value;
|
||||
else
|
||||
throw new InvalidOperationException("RabbitMqConnectionFactory is not initialized. Call InitAsync() before using this method.");
|
||||
}
|
||||
|
||||
public RabbitMqConnectionFactory(IOptions<RabbitMqConfiguration> config)
|
||||
{
|
||||
_config = config.Value;
|
||||
_lazyConnectionProvider = new (async () =>
|
||||
{
|
||||
_cancellationToken?.ThrowIfCancellationRequested();
|
||||
var factory = new ConnectionFactory
|
||||
{
|
||||
HostName = _config.HostName,
|
||||
Port = _config.Port,
|
||||
UserName = _config.UserName,
|
||||
Password = _config.Password,
|
||||
VirtualHost = _config.VirtualHost,
|
||||
AutomaticRecoveryEnabled = _config.AutomaticRecoveryEnabled,
|
||||
NetworkRecoveryInterval = TimeSpan.FromSeconds(_config.NetworkRecoveryIntervalSeconds),
|
||||
};
|
||||
|
||||
_connection = await factory.CreateConnectionAsync((CancellationToken)_cancellationToken!);
|
||||
return _connection;
|
||||
});
|
||||
}
|
||||
|
||||
public async Task InitAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
_cancellationToken = cancellationToken;
|
||||
_ = await GetConnectionAsync();
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (_connection is not null)
|
||||
{
|
||||
await _connection.CloseAsync();
|
||||
await _connection.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using DigitalData.MessagingService.Application.Common.Interfaces;
|
||||
using DigitalData.MessagingService.Infrastructure.Queue;
|
||||
using DigitalData.MessagingService.RabbitMQ;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user