Skip to content

Commit

Permalink
fix: Use correct connection string
Browse files Browse the repository at this point in the history
  • Loading branch information
alex289 committed Nov 14, 2024
1 parent 2a048b6 commit d9ae278
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
34 changes: 21 additions & 13 deletions CleanArchitecture.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,32 +30,41 @@
.AddDbContextCheck<ApplicationDbContext>()
.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<ApplicationDbContext>(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();
Expand All @@ -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";
});
}
Expand Down
8 changes: 4 additions & 4 deletions CleanArchitecture.AppHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
.WithManagementPlugin();

var sqlServer = builder.AddSqlServer("SqlServer");
var db = sqlServer.AddDatabase("Database", "clean-architecture");

builder.AddProject<Projects.CleanArchitecture_Api>("CleanArchitecture.Api")
.WithHttpsEndpoint(17270)
.WithHealthCheck("Api Health")
builder.AddProject<Projects.CleanArchitecture_Api>("CleanArchitecture-Api")
//.WithHealthCheck("Api Health")
.WithOtlpExporter()
.WithReference(redis)
.WaitFor(redis)
.WithReference(rabbitMq)
.WaitFor(rabbitMq)
.WithReference(sqlServer)
.WithReference(db)
.WaitFor(sqlServer);

builder.Build().Run();
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,24 +15,23 @@ 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<EventStoreDbContext>(
options =>
{
options.UseSqlServer(
configuration.GetConnectionString(connectionStringName),
connectionString,
b => b.MigrationsAssembly(migrationsAssemblyName));
});

services.AddDbContext<DomainNotificationStoreDbContext>(
options =>
{
options.UseSqlServer(
configuration.GetConnectionString(connectionStringName),
connectionString,
b => b.MigrationsAssembly(migrationsAssemblyName));
});

Expand Down

0 comments on commit d9ae278

Please sign in to comment.