From d9ae278caf7732259c5cd9925cfd942f22059f41 Mon Sep 17 00:00:00 2001 From: alex289 Date: Thu, 14 Nov 2024 22:15:14 +0100 Subject: [PATCH] fix: Use correct connection string --- CleanArchitecture.Api/Program.cs | 34 ++++++++++++------- CleanArchitecture.AppHost/Program.cs | 8 ++--- .../Extensions/ServiceCollectionExtensions.cs | 8 ++--- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/CleanArchitecture.Api/Program.cs b/CleanArchitecture.Api/Program.cs index 95c2bb1..5efc5d6 100644 --- a/CleanArchitecture.Api/Program.cs +++ b/CleanArchitecture.Api/Program.cs @@ -12,7 +12,6 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; @@ -31,32 +30,41 @@ .AddDbContextCheck() .AddApplicationStatus(); +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 redisConnectionString = isAspire ? builder.Configuration["ConnectionStrings:Redis"] : builder.Configuration["RedisHostName"]; + +var dbConnectionString = isAspire ? builder.Configuration["ConnectionStrings:Database"] : builder.Configuration["ConnectionStrings:DefaultConnection"]; + 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"]; - builder.Services .AddHealthChecks() - .AddSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")!) - .AddRedis(builder.Configuration["RedisHostName"]!, "Redis") + .AddSqlServer(dbConnectionString!) + .AddRedis(redisConnectionString!, "Redis") .AddRabbitMQ( - $"amqp://{rabbitUser}:{rabbitPass}@{rabbitHost}:{rabbitPort}", + rabbitConnectionString!, name: "RabbitMQ"); } builder.Services.AddDbContext(options => { options.UseLazyLoadingProxies(); - options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"), + options.UseSqlServer(dbConnectionString, b => b.MigrationsAssembly("CleanArchitecture.Infrastructure")); }); builder.Services.AddSwagger(); builder.Services.AddAuth(builder.Configuration); -builder.Services.AddInfrastructure(builder.Configuration, "CleanArchitecture.Infrastructure"); +builder.Services.AddInfrastructure("CleanArchitecture.Infrastructure", dbConnectionString!); builder.Services.AddQueryHandlers(); builder.Services.AddServices(); builder.Services.AddSortProviders(); @@ -76,11 +84,11 @@ console.IncludeScopes = true; })); -if (builder.Environment.IsProduction() || !string.IsNullOrWhiteSpace(builder.Configuration["RedisHostName"])) +if (builder.Environment.IsProduction() || !string.IsNullOrWhiteSpace(redisConnectionString)) { builder.Services.AddStackExchangeRedisCache(options => { - options.Configuration = builder.Configuration["RedisHostName"]; + options.Configuration = redisConnectionString; options.InstanceName = "clean-architecture"; }); } diff --git a/CleanArchitecture.AppHost/Program.cs b/CleanArchitecture.AppHost/Program.cs index 261b990..737d164 100644 --- a/CleanArchitecture.AppHost/Program.cs +++ b/CleanArchitecture.AppHost/Program.cs @@ -11,16 +11,16 @@ .WithManagementPlugin(); var sqlServer = builder.AddSqlServer("SqlServer"); +var db = sqlServer.AddDatabase("Database", "clean-architecture"); -builder.AddProject("CleanArchitecture.Api") - .WithHttpsEndpoint(17270) - .WithHealthCheck("Api Health") +builder.AddProject("CleanArchitecture-Api") + //.WithHealthCheck("Api Health") .WithOtlpExporter() .WithReference(redis) .WaitFor(redis) .WithReference(rabbitMq) .WaitFor(rabbitMq) - .WithReference(sqlServer) + .WithReference(db) .WaitFor(sqlServer); builder.Build().Run(); diff --git a/CleanArchitecture.Infrastructure/Extensions/ServiceCollectionExtensions.cs b/CleanArchitecture.Infrastructure/Extensions/ServiceCollectionExtensions.cs index ad8ebac..994058f 100644 --- a/CleanArchitecture.Infrastructure/Extensions/ServiceCollectionExtensions.cs +++ b/CleanArchitecture.Infrastructure/Extensions/ServiceCollectionExtensions.cs @@ -7,7 +7,6 @@ using CleanArchitecture.Infrastructure.Repositories; using MediatR; using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; namespace CleanArchitecture.Infrastructure.Extensions; @@ -16,16 +15,15 @@ public static class ServiceCollectionExtensions { public static IServiceCollection AddInfrastructure( this IServiceCollection services, - IConfiguration configuration, string migrationsAssemblyName, - string connectionStringName = "DefaultConnection") + string connectionString) { // Add event store db context services.AddDbContext( options => { options.UseSqlServer( - configuration.GetConnectionString(connectionStringName), + connectionString, b => b.MigrationsAssembly(migrationsAssemblyName)); }); @@ -33,7 +31,7 @@ public static IServiceCollection AddInfrastructure( options => { options.UseSqlServer( - configuration.GetConnectionString(connectionStringName), + connectionString, b => b.MigrationsAssembly(migrationsAssemblyName)); });