From 07d72758603ec61819d05f40dfdbf0f12c974f99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Michel?= Date: Wed, 6 Apr 2022 14:28:57 +0200 Subject: [PATCH] Fix docker environment issues (#694) --- src/BlazorAdmin/BlazorAdmin.csproj | 10 ++--- src/BlazorShared/BlazorShared.csproj | 2 +- .../Identity/AppIdentityDbContextSeed.cs | 9 +++- src/Infrastructure/Infrastructure.csproj | 8 ++-- src/PublicApi/Program.cs | 43 ++++++++++--------- src/PublicApi/PublicApi.csproj | 26 +++++------ src/Web/Program.cs | 43 ++++++++++--------- src/Web/Web.csproj | 18 ++++---- tests/FunctionalTests/FunctionalTests.csproj | 6 +-- .../IntegrationTests/IntegrationTests.csproj | 6 +-- .../PublicApiIntegrationTests.csproj | 8 ++-- ...appsettings.json => appsettings.test.json} | 0 tests/UnitTests/UnitTests.csproj | 4 +- 13 files changed, 96 insertions(+), 87 deletions(-) rename tests/PublicApiIntegrationTests/{appsettings.json => appsettings.test.json} (100%) diff --git a/src/BlazorAdmin/BlazorAdmin.csproj b/src/BlazorAdmin/BlazorAdmin.csproj index 20c38f6..d30ffce 100644 --- a/src/BlazorAdmin/BlazorAdmin.csproj +++ b/src/BlazorAdmin/BlazorAdmin.csproj @@ -7,11 +7,11 @@ - - - - - + + + + + diff --git a/src/BlazorShared/BlazorShared.csproj b/src/BlazorShared/BlazorShared.csproj index 9717973..66420f2 100644 --- a/src/BlazorShared/BlazorShared.csproj +++ b/src/BlazorShared/BlazorShared.csproj @@ -8,7 +8,7 @@ - + diff --git a/src/Infrastructure/Identity/AppIdentityDbContextSeed.cs b/src/Infrastructure/Identity/AppIdentityDbContextSeed.cs index a3315de..3531005 100644 --- a/src/Infrastructure/Identity/AppIdentityDbContextSeed.cs +++ b/src/Infrastructure/Identity/AppIdentityDbContextSeed.cs @@ -1,13 +1,20 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Identity; +using Microsoft.EntityFrameworkCore; using Microsoft.eShopWeb.ApplicationCore.Constants; namespace Microsoft.eShopWeb.Infrastructure.Identity; public class AppIdentityDbContextSeed { - public static async Task SeedAsync(UserManager userManager, RoleManager roleManager) + public static async Task SeedAsync(AppIdentityDbContext identityDbContext, UserManager userManager, RoleManager roleManager) { + + if (identityDbContext.Database.IsSqlServer()) + { + identityDbContext.Database.Migrate(); + } + await roleManager.CreateAsync(new IdentityRole(BlazorShared.Authorization.Constants.Roles.ADMINISTRATORS)); var defaultUser = new ApplicationUser { UserName = "demouser@microsoft.com", Email = "demouser@microsoft.com" }; diff --git a/src/Infrastructure/Infrastructure.csproj b/src/Infrastructure/Infrastructure.csproj index f627543..5d6ad8d 100644 --- a/src/Infrastructure/Infrastructure.csproj +++ b/src/Infrastructure/Infrastructure.csproj @@ -7,10 +7,10 @@ - - - - + + + + diff --git a/src/PublicApi/Program.cs b/src/PublicApi/Program.cs index aeb11bc..0e10976 100644 --- a/src/PublicApi/Program.cs +++ b/src/PublicApi/Program.cs @@ -31,7 +31,7 @@ var builder = WebApplication.CreateBuilder(args); builder.Services.AddEndpoints(); //Use to force loading of appsettings.json of test project -builder.Configuration.AddConfigurationFile(); +builder.Configuration.AddConfigurationFile("appsettings.test.json"); builder.Logging.AddConsole(); Microsoft.eShopWeb.Infrastructure.Dependencies.ConfigureServices(builder.Configuration, builder.Services); @@ -129,6 +129,27 @@ var app = builder.Build(); app.Logger.LogInformation("PublicApi App created..."); +app.Logger.LogInformation("Seeding Database..."); + +using (var scope = app.Services.CreateScope()) +{ + var scopedProvider = scope.ServiceProvider; + try + { + var catalogContext = scopedProvider.GetRequiredService(); + await CatalogContextSeed.SeedAsync(catalogContext, app.Logger); + + var userManager = scopedProvider.GetRequiredService>(); + var roleManager = scopedProvider.GetRequiredService>(); + var identityContext = scopedProvider.GetRequiredService(); + await AppIdentityDbContextSeed.SeedAsync(identityContext, userManager, roleManager); + } + catch (Exception ex) + { + app.Logger.LogError(ex, "An error occurred seeding the DB."); + } +} + if (app.Environment.IsDevelopment()) { app.UseDeveloperExceptionPage(); @@ -159,26 +180,6 @@ app.UseEndpoints(endpoints => endpoints.MapControllers(); }); -app.Logger.LogInformation("Seeding Database..."); - -using (var scope = app.Services.CreateScope()) -{ - var scopedProvider = scope.ServiceProvider; - try - { - var catalogContext = scopedProvider.GetRequiredService(); - await CatalogContextSeed.SeedAsync(catalogContext, app.Logger); - - var userManager = scopedProvider.GetRequiredService>(); - var roleManager = scopedProvider.GetRequiredService>(); - await AppIdentityDbContextSeed.SeedAsync(userManager, roleManager); - } - catch (Exception ex) - { - app.Logger.LogError(ex, "An error occurred seeding the DB."); - } -} - app.MapEndpoints(); app.Logger.LogInformation("LAUNCHING PublicApi"); app.Run(); diff --git a/src/PublicApi/PublicApi.csproj b/src/PublicApi/PublicApi.csproj index 4bf1dd6..0622c74 100644 --- a/src/PublicApi/PublicApi.csproj +++ b/src/PublicApi/PublicApi.csproj @@ -13,24 +13,24 @@ - - - - - - - - - - - + + + + + + + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - + - + diff --git a/src/Web/Program.cs b/src/Web/Program.cs index 2e78ab3..e1181e2 100644 --- a/src/Web/Program.cs +++ b/src/Web/Program.cs @@ -98,6 +98,27 @@ var app = builder.Build(); app.Logger.LogInformation("App created..."); +app.Logger.LogInformation("Seeding Database..."); + +using (var scope = app.Services.CreateScope()) +{ + var scopedProvider = scope.ServiceProvider; + try + { + var catalogContext = scopedProvider.GetRequiredService(); + await CatalogContextSeed.SeedAsync(catalogContext, app.Logger); + + var userManager = scopedProvider.GetRequiredService>(); + var roleManager = scopedProvider.GetRequiredService>(); + var identityContext = scopedProvider.GetRequiredService(); + await AppIdentityDbContextSeed.SeedAsync(identityContext, userManager, roleManager); + } + catch (Exception ex) + { + app.Logger.LogError(ex, "An error occurred seeding the DB."); + } +} + var catalogBaseUrl = builder.Configuration.GetValue(typeof(string), "CatalogBaseUrl") as string; if (!string.IsNullOrEmpty(catalogBaseUrl)) { @@ -126,7 +147,7 @@ app.UseHealthChecks("/health", await context.Response.WriteAsync(result); } }); -if (app.Environment.IsDevelopment()) +if (app.Environment.IsDevelopment() || app.Environment.EnvironmentName == "Docker") { app.Logger.LogInformation("Adding Development middleware..."); app.UseDeveloperExceptionPage(); @@ -160,25 +181,5 @@ app.UseEndpoints(endpoints => endpoints.MapFallbackToFile("index.html"); }); -app.Logger.LogInformation("Seeding Database..."); - -using (var scope = app.Services.CreateScope()) -{ - var scopedProvider = scope.ServiceProvider; - try - { - var catalogContext = scopedProvider.GetRequiredService(); - await CatalogContextSeed.SeedAsync(catalogContext, app.Logger); - - var userManager = scopedProvider.GetRequiredService>(); - var roleManager = scopedProvider.GetRequiredService>(); - await AppIdentityDbContextSeed.SeedAsync(userManager, roleManager); - } - catch (Exception ex) - { - app.Logger.LogError(ex, "An error occurred seeding the DB."); - } -} - app.Logger.LogInformation("LAUNCHING"); app.Run(); diff --git a/src/Web/Web.csproj b/src/Web/Web.csproj index 681955a..d0b9a3c 100644 --- a/src/Web/Web.csproj +++ b/src/Web/Web.csproj @@ -20,20 +20,20 @@ - - + + - - - - - - + + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/tests/FunctionalTests/FunctionalTests.csproj b/tests/FunctionalTests/FunctionalTests.csproj index 1c6a574..4a3072a 100644 --- a/tests/FunctionalTests/FunctionalTests.csproj +++ b/tests/FunctionalTests/FunctionalTests.csproj @@ -15,14 +15,14 @@ - - + + all runtime; build; native; contentfiles; analyzers - + diff --git a/tests/IntegrationTests/IntegrationTests.csproj b/tests/IntegrationTests/IntegrationTests.csproj index 98dd06e..3f9c0ba 100644 --- a/tests/IntegrationTests/IntegrationTests.csproj +++ b/tests/IntegrationTests/IntegrationTests.csproj @@ -7,9 +7,9 @@ - - - + + + all diff --git a/tests/PublicApiIntegrationTests/PublicApiIntegrationTests.csproj b/tests/PublicApiIntegrationTests/PublicApiIntegrationTests.csproj index 3dcb235..e73f5f3 100644 --- a/tests/PublicApiIntegrationTests/PublicApiIntegrationTests.csproj +++ b/tests/PublicApiIntegrationTests/PublicApiIntegrationTests.csproj @@ -8,11 +8,11 @@ - + - + Always true PreserveNewest @@ -20,8 +20,8 @@ - - + + diff --git a/tests/PublicApiIntegrationTests/appsettings.json b/tests/PublicApiIntegrationTests/appsettings.test.json similarity index 100% rename from tests/PublicApiIntegrationTests/appsettings.json rename to tests/PublicApiIntegrationTests/appsettings.test.json diff --git a/tests/UnitTests/UnitTests.csproj b/tests/UnitTests/UnitTests.csproj index 8ecd3d4..fdf0d2d 100644 --- a/tests/UnitTests/UnitTests.csproj +++ b/tests/UnitTests/UnitTests.csproj @@ -11,8 +11,8 @@ - - + + all