diff --git a/src/Services/Catalog/Catalog.API/CustomExtensionMethods.cs b/src/Services/Catalog/Catalog.API/CustomExtensionMethods.cs index 962108297..7779d791d 100644 --- a/src/Services/Catalog/Catalog.API/CustomExtensionMethods.cs +++ b/src/Services/Catalog/Catalog.API/CustomExtensionMethods.cs @@ -6,14 +6,10 @@ public static class CustomExtensionMethods { var hcBuilder = services.AddHealthChecks(); - if (configuration.GetConnectionString("CatalogDB") is string connectionString) - { - hcBuilder - .AddSqlServer( - connectionString, - name: "CatalogDB-check", - tags: new string[] { "live", "ready" }); - } + hcBuilder + .AddSqlServer(_ => configuration.GetConnectionString("CatalogDB"), + name: "CatalogDB-check", + tags: new string[] { "live", "ready" }); var accountName = configuration["AzureStorageAccountName"]; var accountKey = configuration["AzureStorageAccountKey"]; diff --git a/src/Services/Catalog/Catalog.API/Extensions/WebHostExtensions.cs b/src/Services/Catalog/Catalog.API/Extensions/WebHostExtensions.cs deleted file mode 100644 index d4a452aed..000000000 --- a/src/Services/Catalog/Catalog.API/Extensions/WebHostExtensions.cs +++ /dev/null @@ -1,68 +0,0 @@ -namespace Microsoft.eShopOnContainers.Services.Catalog.API.Extensions; - -public static class WebHostExtensions -{ - public static bool IsInKubernetes(this IServiceProvider services) - { - var cfg = services.GetService(); - var orchestratorType = cfg.GetValue("OrchestratorType"); - return orchestratorType?.ToUpper() == "K8S"; - } - - public static IServiceProvider MigrateDbContext(this IServiceProvider services, Action seeder) where TContext : DbContext - { - var underK8s = services.IsInKubernetes(); - - using var scope = services.CreateScope(); - var scopedServices = scope.ServiceProvider; - - var logger = scopedServices.GetRequiredService>(); - - var context = scopedServices.GetService(); - - try - { - logger.LogInformation("Migrating database associated with context {DbContextName}", typeof(TContext).Name); - - if (underK8s) - { - InvokeSeeder(seeder, context, scopedServices); - } - else - { - var retry = Policy.Handle() - .WaitAndRetry(new TimeSpan[] - { - TimeSpan.FromSeconds(3), - TimeSpan.FromSeconds(5), - TimeSpan.FromSeconds(8), - }); - - //if the sql server container is not created on run docker compose this - //migration can't fail for network related exception. The retry options for DbContext only - //apply to transient exceptions - // Note that this is NOT applied when running some orchestrators (let the orchestrator to recreate the failing service) - retry.Execute(() => InvokeSeeder(seeder, context, scopedServices)); - } - - logger.LogInformation("Migrated database associated with context {DbContextName}", typeof(TContext).Name); - } - catch (Exception ex) - { - logger.LogError(ex, "An error occurred while migrating the database used on context {DbContextName}", typeof(TContext).Name); - if (underK8s) - { - throw; // Rethrow under k8s because we rely on k8s to re-run the pod - } - } - - return services; - } - - private static void InvokeSeeder(Action seeder, TContext context, IServiceProvider services) - where TContext : DbContext - { - context.Database.Migrate(); - seeder(context, services); - } -} diff --git a/src/Services/Catalog/Catalog.API/Program.cs b/src/Services/Catalog/Catalog.API/Program.cs index 9559e57cb..f15724b83 100644 --- a/src/Services/Catalog/Catalog.API/Program.cs +++ b/src/Services/Catalog/Catalog.API/Program.cs @@ -41,6 +41,7 @@ var eventBus = app.Services.GetRequiredService(); eventBus.Subscribe(); eventBus.Subscribe(); +// REVIEW: This is done fore development east but shouldn't be here in production using (var scope = app.Services.CreateScope()) { var context = scope.ServiceProvider.GetRequiredService(); diff --git a/src/Services/Catalog/Catalog.API/appsettings.json b/src/Services/Catalog/Catalog.API/appsettings.json index 427b0bd95..937a2a3a8 100644 --- a/src/Services/Catalog/Catalog.API/appsettings.json +++ b/src/Services/Catalog/Catalog.API/appsettings.json @@ -16,8 +16,7 @@ } }, "ConnectionStrings": { - "EventBus": "localhost", - "CatalogDB": "" + "EventBus": "localhost" }, "EventBus": { "SubscriptionClientName": "Catalog", diff --git a/src/Services/Catalog/Catalog.FunctionalTests/CatalogScenarioBase.cs b/src/Services/Catalog/Catalog.FunctionalTests/CatalogScenarioBase.cs index c8bd8806b..63324cc2f 100644 --- a/src/Services/Catalog/Catalog.FunctionalTests/CatalogScenarioBase.cs +++ b/src/Services/Catalog/Catalog.FunctionalTests/CatalogScenarioBase.cs @@ -7,29 +7,11 @@ public class CatalogScenariosBase public TestServer CreateServer() { var factory = new CatalogApplication(); - return factory.CreateServer(); + return factory.Server; } private class CatalogApplication : WebApplicationFactory { - public TestServer CreateServer() - { - Services - .MigrateDbContext((context, services) => - { - var env = services.GetService(); - var settings = services.GetService>(); - var logger = services.GetService>(); - - new CatalogContextSeed() - .SeedAsync(context, env, settings, logger) - .Wait(); - }) - .MigrateDbContext((_, __) => { }); - - return Server; - } - protected override IHost CreateHost(IHostBuilder builder) { builder.ConfigureAppConfiguration(c => diff --git a/src/Services/Catalog/Catalog.FunctionalTests/appsettings.json b/src/Services/Catalog/Catalog.FunctionalTests/appsettings.json index 65abc02e2..2489ef0a1 100644 --- a/src/Services/Catalog/Catalog.FunctionalTests/appsettings.json +++ b/src/Services/Catalog/Catalog.FunctionalTests/appsettings.json @@ -1,10 +1,5 @@ { "ExternalCatalogBaseUrl": "http://localhost:5101", - "Identity": { - "Url": "http://localhost:5105", - "ExternalUrl": "http://localhost:5105", - "Audience": "catalog" - }, "isTest": "true", "PicBaseUrl": "http://localhost:5101/api/v1/catalog/items/[0]/pic/", @@ -13,7 +8,6 @@ }, "EventBus": { - "SubscriptionClientName": "Catalog", - "ConnectionString": "localhost" + "SubscriptionClientName": "Catalog" } }