From c29aa41e43e72ba9bc5f3d1ba3652c66da3e91a4 Mon Sep 17 00:00:00 2001 From: JasonTaylorDev Date: Thu, 28 Nov 2024 16:18:40 +1000 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Bug=20fixes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AppHost/Program.cs | 4 +++ src/Infrastructure/DependencyInjection.cs | 28 ++++++++----------- .../CustomWebApplicationFactory.cs | 12 +++++--- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/AppHost/Program.cs b/src/AppHost/Program.cs index 369b04325..2c80135ef 100644 --- a/src/AppHost/Program.cs +++ b/src/AppHost/Program.cs @@ -3,6 +3,7 @@ // Note: To run without Docker, simply remove sql and database: // builder.AddProject("web"); +#if (!UseSqlite) var sql = builder.AddSqlServer("sql"); var database = sql.AddDatabase("CleanArchitectureDb"); @@ -10,5 +11,8 @@ builder.AddProject("web") .WaitFor(database) .WithReference(database); +#else +builder.AddProject("web"); +#endif builder.Build().Run(); diff --git a/src/Infrastructure/DependencyInjection.cs b/src/Infrastructure/DependencyInjection.cs index 263a0b491..146df521d 100644 --- a/src/Infrastructure/DependencyInjection.cs +++ b/src/Infrastructure/DependencyInjection.cs @@ -15,34 +15,30 @@ public static class DependencyInjection { public static void AddInfrastructureServices(this IHostApplicationBuilder builder) { - builder.Services.AddScoped(); - builder.Services.AddScoped(); - var connectionString = builder.Configuration.GetConnectionString("CleanArchitectureDb"); Guard.Against.Null(connectionString, message: "Connection string 'CleanArchitectureDb' not found."); - -#if (UseSqlite) + + builder.Services.AddScoped(); + builder.Services.AddScoped(); + +#if (UseSqlite) builder.Services.AddDbContext((sp, options) => { options.AddInterceptors(sp.GetServices()); options.UseSqlite(connectionString); - }); -#else - #if (UseAspire) - builder.AddSqlServerDbContext("CleanArchitectureDb", null, options => - { - var sp = builder.Services.BuildServiceProvider(); - options.AddInterceptors(sp.GetServices()); - }); - #else + }); +#else builder.Services.AddDbContext((sp, options) => { options.AddInterceptors(sp.GetServices()); options.UseSqlServer(connectionString); }); - #endif -#endif + #if (UseAspire) + builder.EnrichSqlServerDbContext(); + #endif +#endif + builder.Services.AddScoped(provider => provider.GetRequiredService()); builder.Services.AddScoped(); diff --git a/tests/Application.FunctionalTests/CustomWebApplicationFactory.cs b/tests/Application.FunctionalTests/CustomWebApplicationFactory.cs index a53d5400f..8ac74fa6f 100644 --- a/tests/Application.FunctionalTests/CustomWebApplicationFactory.cs +++ b/tests/Application.FunctionalTests/CustomWebApplicationFactory.cs @@ -24,23 +24,27 @@ public CustomWebApplicationFactory(DbConnection connection) protected override void ConfigureWebHost(IWebHostBuilder builder) { +#if (UseAspire) + builder.UseSetting("ConnectionStrings:CleanArchitectureDb", _connection.ConnectionString); +#endif builder.ConfigureTestServices(services => { services .RemoveAll() .AddTransient(provider => Mock.Of(s => s.Id == GetUserId())); - +#if (!UseAspire || UseSqlite) services .RemoveAll>() .AddDbContext((sp, options) => { options.AddInterceptors(sp.GetServices()); -#if (UseSqlite) + #if (UseSqlite) options.UseSqlite(_connection); -#else + #else options.UseSqlServer(_connection); -#endif + #endif }); +#endif }); } }