0
0
mirror of https://github.com/alex289/CleanArchitecture.git synced 2025-06-29 18:21:08 +00:00

fix: Use correct rabbit and redis connection

This commit is contained in:
Alexander Konietzko 2024-08-05 11:39:11 +02:00
parent 54062982d7
commit 55951f8ba1
No known key found for this signature in database
GPG Key ID: BA6905F37AEC2B5B
9 changed files with 35 additions and 8 deletions

View File

@ -31,6 +31,7 @@ builder.Services
if (builder.Environment.IsProduction())
{
var rabbitHost = builder.Configuration["RabbitMQ:Host"];
var rabbitPort = builder.Configuration["RabbitMQ:Port"];
var rabbitUser = builder.Configuration["RabbitMQ:Username"];
var rabbitPass = builder.Configuration["RabbitMQ:Password"];
@ -39,7 +40,7 @@ if (builder.Environment.IsProduction())
.AddSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")!)
.AddRedis(builder.Configuration["RedisHostName"]!, "Redis")
.AddRabbitMQ(
$"amqp://{rabbitUser}:{rabbitPass}@{rabbitHost}",
$"amqp://{rabbitUser}:{rabbitPass}@{rabbitHost}:{rabbitPort}",
name: "RabbitMQ");
}

View File

@ -16,6 +16,7 @@
"RedisHostName": "",
"RabbitMQ": {
"Host": "localhost",
"Port": 5672,
"Username": "guest",
"Password": "guest",
"Enabled": "True"

View File

@ -8,6 +8,7 @@
"RedisHostName": "",
"RabbitMQ": {
"Host": "localhost",
"Port": 5672,
"Username": "guest",
"Password": "guest",
"Enabled": "True"

View File

@ -17,6 +17,7 @@
"RedisHostName": "redis",
"RabbitMQ": {
"Host": "rabbitmq",
"Port": 5672,
"Username": "admin",
"Password": "DOIA9234JF",
"Enabled": "True"

View File

@ -3,6 +3,7 @@ namespace CleanArchitecture.Domain.Rabbitmq;
public sealed class RabbitMqConfiguration
{
public string Host { get; set; } = string.Empty;
public int Port { get; set; }
public bool Enabled { get; set; }
public string Username { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;

View File

@ -39,6 +39,7 @@ public sealed class RabbitMqHandler : BackgroundService
{
AutomaticRecoveryEnabled = true,
HostName = configuration.Host,
Port = configuration.Port,
UserName = configuration.Username,
Password = configuration.Password,
DispatchConsumersAsync = true

View File

@ -4,5 +4,5 @@ public static class Configuration
{
public const int RedisPort = 6379;
public const int MsSqlPort = 1433;
public const int RabbitMqPort = 5673;
public const int RabbitMqPort = 5672;
}

View File

@ -22,6 +22,8 @@ internal class GlobalSetupFixture
.Build();
public static RabbitMqContainer RabbitContainer { get; } = new RabbitMqBuilder()
.WithUsername("guest")
.WithPassword("guest")
.WithPortBinding(Configuration.RabbitMqPort, assignRandomHostPort: true)
.Build();

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using CleanArchitecture.Domain.Rabbitmq;
using CleanArchitecture.IntegrationTests.Constants;
using CleanArchitecture.IntegrationTests.Infrastructure.Auth;
using Microsoft.AspNetCore.Hosting;
@ -33,22 +34,27 @@ public sealed class CleanArchitectureWebApplicationFactory : WebApplicationFacto
base.ConfigureWebHost(builder);
builder.ConfigureAppConfiguration(configuration =>
var configuration = new ConfigurationBuilder()
.Build();
builder.ConfigureAppConfiguration(configurationBuilder =>
{
var redisPort = GlobalSetupFixture.RedisContainer.GetMappedPublicPort(Configuration.RedisPort);
var rabbitPort = GlobalSetupFixture.RabbitContainer.GetMappedPublicPort(Configuration.RabbitMqPort);
configuration.AddInMemoryCollection([
configurationBuilder.AddInMemoryCollection([
new KeyValuePair<string, string?>(
"ConnectionStrings:DefaultConnection",
GlobalSetupFixture.DatabaseConnectionString),
new KeyValuePair<string, string?>(
"RedisStackExchange:RedisConfigString",
$"localhost:{redisPort},abortConnect=true"),
"RedisHostName",
$"localhost:{redisPort}"),
new KeyValuePair<string, string?>(
"RabbitMQ:Host",
$"localhost:{rabbitPort}")
"RabbitMQ:Port",
rabbitPort.ToString())
]);
configuration = configurationBuilder.Build();
});
builder.ConfigureServices(services =>
@ -66,6 +72,19 @@ public sealed class CleanArchitectureWebApplicationFactory : WebApplicationFacto
using var scope = sp.CreateScope();
var scopedServices = scope.ServiceProvider;
// Readd rabbitmq options to use the correct port
var rabbitMq = new RabbitMqConfiguration();
configuration.Bind("RabbitMQ", rabbitMq);
services.AddSingleton(rabbitMq);
// Readd IDistributedCache to replace the memory cache with redis
services.AddStackExchangeRedisCache(options =>
{
options.Configuration = configuration["RedisHostName"];
options.InstanceName = "clean-architecture";
});
_registerCustomServicesHandler?.Invoke(services, sp, scopedServices);
});
}