mirror of
https://github.com/alex289/CleanArchitecture.git
synced 2025-06-30 10:33:43 +00:00
feat: Fix rabbitmq registration
This commit is contained in:
parent
425da0b450
commit
693ca4589d
41
CleanArchitecture.Api/Extensions/ConfigurationExtensions.cs
Normal file
41
CleanArchitecture.Api/Extensions/ConfigurationExtensions.cs
Normal file
@ -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 ?? "",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -32,16 +32,8 @@ builder.Services
|
|||||||
|
|
||||||
var isAspire = builder.Configuration["ASPIRE_ENABLED"] == "true";
|
var isAspire = builder.Configuration["ASPIRE_ENABLED"] == "true";
|
||||||
|
|
||||||
var rabbitHost = builder.Configuration["RabbitMQ:Host"];
|
var rabbitConfiguration = builder.Configuration.GetRabbitMqConfiguration();
|
||||||
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 redisConnectionString = isAspire ? builder.Configuration["ConnectionStrings:Redis"] : builder.Configuration["RedisHostName"];
|
var redisConnectionString = isAspire ? builder.Configuration["ConnectionStrings:Redis"] : builder.Configuration["RedisHostName"];
|
||||||
|
|
||||||
var dbConnectionString = isAspire ? builder.Configuration["ConnectionStrings:Database"] : builder.Configuration["ConnectionStrings:DefaultConnection"];
|
var dbConnectionString = isAspire ? builder.Configuration["ConnectionStrings:Database"] : builder.Configuration["ConnectionStrings:DefaultConnection"];
|
||||||
|
|
||||||
if (builder.Environment.IsProduction())
|
if (builder.Environment.IsProduction())
|
||||||
@ -51,7 +43,7 @@ if (builder.Environment.IsProduction())
|
|||||||
.AddSqlServer(dbConnectionString!)
|
.AddSqlServer(dbConnectionString!)
|
||||||
.AddRedis(redisConnectionString!, "Redis")
|
.AddRedis(redisConnectionString!, "Redis")
|
||||||
.AddRabbitMQ(
|
.AddRabbitMQ(
|
||||||
rabbitConnectionString!,
|
rabbitConfiguration.ConnectionString,
|
||||||
name: "RabbitMQ");
|
name: "RabbitMQ");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,7 +64,7 @@ builder.Services.AddCommandHandlers();
|
|||||||
builder.Services.AddNotificationHandlers();
|
builder.Services.AddNotificationHandlers();
|
||||||
builder.Services.AddApiUser();
|
builder.Services.AddApiUser();
|
||||||
|
|
||||||
builder.Services.AddRabbitMqHandler(builder.Configuration, "RabbitMQ");
|
builder.Services.AddRabbitMqHandler(rabbitConfiguration);
|
||||||
|
|
||||||
builder.Services.AddHostedService<SetInactiveUsersService>();
|
builder.Services.AddHostedService<SetInactiveUsersService>();
|
||||||
|
|
||||||
|
@ -7,12 +7,9 @@ public static class ServiceCollectionExtensions
|
|||||||
{
|
{
|
||||||
public static IServiceCollection AddRabbitMqHandler(
|
public static IServiceCollection AddRabbitMqHandler(
|
||||||
this IServiceCollection services,
|
this IServiceCollection services,
|
||||||
IConfiguration configuration,
|
RabbitMqConfiguration configuration)
|
||||||
string rabbitMqConfigSection)
|
|
||||||
{
|
{
|
||||||
var rabbitMq = new RabbitMqConfiguration();
|
services.AddSingleton(configuration);
|
||||||
configuration.Bind(rabbitMqConfigSection, rabbitMq);
|
|
||||||
services.AddSingleton(rabbitMq);
|
|
||||||
|
|
||||||
services.AddSingleton<RabbitMqHandler>();
|
services.AddSingleton<RabbitMqHandler>();
|
||||||
services.AddHostedService(serviceProvider => serviceProvider.GetService<RabbitMqHandler>()!);
|
services.AddHostedService(serviceProvider => serviceProvider.GetService<RabbitMqHandler>()!);
|
||||||
|
@ -7,4 +7,6 @@ public sealed class RabbitMqConfiguration
|
|||||||
public bool Enabled { get; set; }
|
public bool Enabled { get; set; }
|
||||||
public string Username { get; set; } = string.Empty;
|
public string Username { get; set; } = string.Empty;
|
||||||
public string Password { get; set; } = string.Empty;
|
public string Password { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
public string ConnectionString => $"amqp://{Username}:{Password}@{Host}:{Port}";
|
||||||
}
|
}
|
@ -38,6 +38,8 @@ public sealed class RabbitMqHandler : BackgroundService
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_logger.LogInformation("Starting RabbitMQ connection");
|
||||||
|
|
||||||
var factory = new ConnectionFactory
|
var factory = new ConnectionFactory
|
||||||
{
|
{
|
||||||
AutomaticRecoveryEnabled = true,
|
AutomaticRecoveryEnabled = true,
|
||||||
@ -49,6 +51,8 @@ public sealed class RabbitMqHandler : BackgroundService
|
|||||||
|
|
||||||
var connection = await factory.CreateConnectionAsync(cancellationToken);
|
var connection = await factory.CreateConnectionAsync(cancellationToken);
|
||||||
_channel = await connection.CreateChannelAsync(null, cancellationToken);
|
_channel = await connection.CreateChannelAsync(null, cancellationToken);
|
||||||
|
|
||||||
|
await base.StartAsync(cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user