From 693ca4589d558fa71835410e6d0c8c6d4c0d9644 Mon Sep 17 00:00:00 2001 From: alex289 Date: Thu, 21 Nov 2024 22:08:39 +0100 Subject: [PATCH] feat: Fix rabbitmq registration --- .../Extensions/ConfigurationExtensions.cs | 41 +++++++++++++++++++ CleanArchitecture.Api/Program.cs | 14 ++----- .../Extensions/ServiceCollectionExtensions.cs | 7 +--- .../Rabbitmq/RabbitMqConfiguration.cs | 2 + .../Rabbitmq/RabbitMqHandler.cs | 4 ++ 5 files changed, 52 insertions(+), 16 deletions(-) create mode 100644 CleanArchitecture.Api/Extensions/ConfigurationExtensions.cs diff --git a/CleanArchitecture.Api/Extensions/ConfigurationExtensions.cs b/CleanArchitecture.Api/Extensions/ConfigurationExtensions.cs new file mode 100644 index 0000000..bbe88c7 --- /dev/null +++ b/CleanArchitecture.Api/Extensions/ConfigurationExtensions.cs @@ -0,0 +1,41 @@ +using System; +using CleanArchitecture.Domain.Rabbitmq; +using Microsoft.Extensions.Configuration; + +namespace CleanArchitecture.Api.Extensions; + +public static class ConfigurationExtensions +{ + public static RabbitMqConfiguration GetRabbitMqConfiguration( + this IConfiguration configuration) + { + var isAspire = configuration["ASPIRE_ENABLED"] == "true"; + + var rabbitEnabled = configuration["RabbitMQ:Enabled"]; + var rabbitHost = configuration["RabbitMQ:Host"]; + var rabbitPort = configuration["RabbitMQ:Port"]; + var rabbitUser = configuration["RabbitMQ:Username"]; + var rabbitPass = configuration["RabbitMQ:Password"]; + + if (isAspire) + { + rabbitEnabled = "true"; + var connectionString = configuration["ConnectionStrings:RabbitMq"]; + + var rabbitUri = new Uri(connectionString!); + rabbitHost = rabbitUri.Host; + rabbitPort = rabbitUri.Port.ToString(); + rabbitUser = rabbitUri.UserInfo.Split(':')[0]; + rabbitPass = rabbitUri.UserInfo.Split(':')[1]; + } + + return new RabbitMqConfiguration() + { + Host = rabbitHost ?? "", + Port = int.Parse(rabbitPort ?? "0"), + Enabled = bool.Parse(rabbitEnabled ?? "false"), + Username = rabbitUser ?? "", + Password = rabbitPass ?? "", + }; + } +} \ No newline at end of file diff --git a/CleanArchitecture.Api/Program.cs b/CleanArchitecture.Api/Program.cs index 5efc5d6..e0410b8 100644 --- a/CleanArchitecture.Api/Program.cs +++ b/CleanArchitecture.Api/Program.cs @@ -32,16 +32,8 @@ var isAspire = builder.Configuration["ASPIRE_ENABLED"] == "true"; -var rabbitHost = builder.Configuration["RabbitMQ:Host"]; -var rabbitPort = builder.Configuration["RabbitMQ:Port"]; -var rabbitUser = builder.Configuration["RabbitMQ:Username"]; -var rabbitPass = builder.Configuration["RabbitMQ:Password"]; -var rabbitConnectionString = - isAspire ? builder.Configuration["ConnectionStrings:RabbitMq"] : - $"amqp://{rabbitUser}:{rabbitPass}@{rabbitHost}:{rabbitPort}"; - +var rabbitConfiguration = builder.Configuration.GetRabbitMqConfiguration(); var redisConnectionString = isAspire ? builder.Configuration["ConnectionStrings:Redis"] : builder.Configuration["RedisHostName"]; - var dbConnectionString = isAspire ? builder.Configuration["ConnectionStrings:Database"] : builder.Configuration["ConnectionStrings:DefaultConnection"]; if (builder.Environment.IsProduction()) @@ -51,7 +43,7 @@ .AddSqlServer(dbConnectionString!) .AddRedis(redisConnectionString!, "Redis") .AddRabbitMQ( - rabbitConnectionString!, + rabbitConfiguration.ConnectionString, name: "RabbitMQ"); } @@ -72,7 +64,7 @@ builder.Services.AddNotificationHandlers(); builder.Services.AddApiUser(); -builder.Services.AddRabbitMqHandler(builder.Configuration, "RabbitMQ"); +builder.Services.AddRabbitMqHandler(rabbitConfiguration); builder.Services.AddHostedService(); diff --git a/CleanArchitecture.Domain/Rabbitmq/Extensions/ServiceCollectionExtensions.cs b/CleanArchitecture.Domain/Rabbitmq/Extensions/ServiceCollectionExtensions.cs index 42ba1ca..21b7413 100644 --- a/CleanArchitecture.Domain/Rabbitmq/Extensions/ServiceCollectionExtensions.cs +++ b/CleanArchitecture.Domain/Rabbitmq/Extensions/ServiceCollectionExtensions.cs @@ -7,12 +7,9 @@ public static class ServiceCollectionExtensions { public static IServiceCollection AddRabbitMqHandler( this IServiceCollection services, - IConfiguration configuration, - string rabbitMqConfigSection) + RabbitMqConfiguration configuration) { - var rabbitMq = new RabbitMqConfiguration(); - configuration.Bind(rabbitMqConfigSection, rabbitMq); - services.AddSingleton(rabbitMq); + services.AddSingleton(configuration); services.AddSingleton(); services.AddHostedService(serviceProvider => serviceProvider.GetService()!); diff --git a/CleanArchitecture.Domain/Rabbitmq/RabbitMqConfiguration.cs b/CleanArchitecture.Domain/Rabbitmq/RabbitMqConfiguration.cs index e293b6f..b5771e4 100644 --- a/CleanArchitecture.Domain/Rabbitmq/RabbitMqConfiguration.cs +++ b/CleanArchitecture.Domain/Rabbitmq/RabbitMqConfiguration.cs @@ -7,4 +7,6 @@ public sealed class RabbitMqConfiguration public bool Enabled { get; set; } public string Username { get; set; } = string.Empty; public string Password { get; set; } = string.Empty; + + public string ConnectionString => $"amqp://{Username}:{Password}@{Host}:{Port}"; } \ No newline at end of file diff --git a/CleanArchitecture.Domain/Rabbitmq/RabbitMqHandler.cs b/CleanArchitecture.Domain/Rabbitmq/RabbitMqHandler.cs index 4fcb983..2f8b80f 100644 --- a/CleanArchitecture.Domain/Rabbitmq/RabbitMqHandler.cs +++ b/CleanArchitecture.Domain/Rabbitmq/RabbitMqHandler.cs @@ -38,6 +38,8 @@ public override async Task StartAsync(CancellationToken cancellationToken) return; } + _logger.LogInformation("Starting RabbitMQ connection"); + var factory = new ConnectionFactory { AutomaticRecoveryEnabled = true, @@ -49,6 +51,8 @@ public override async Task StartAsync(CancellationToken cancellationToken) var connection = await factory.CreateConnectionAsync(cancellationToken); _channel = await connection.CreateChannelAsync(null, cancellationToken); + + await base.StartAsync(cancellationToken); }