Author | SHA1 | Message | Date |
---|---|---|---|
David Fowler | 493031b11c | Added implict usings | 1 year ago |
@ -1,24 +1,13 @@ | |||
global using CatalogApi; | |||
global using Grpc.Core.Interceptors; | |||
global using System.Text.Json; | |||
global using CatalogApi; | |||
global using Grpc.Core; | |||
global using Grpc.Core.Interceptors; | |||
global using GrpcBasket; | |||
global using Microsoft.AspNetCore.Authorization; | |||
global using Microsoft.AspNetCore.Builder; | |||
global using Microsoft.AspNetCore.Http; | |||
global using Microsoft.AspNetCore.Mvc; | |||
global using Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Config; | |||
global using Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Infrastructure; | |||
global using Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Models; | |||
global using Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Services; | |||
global using Microsoft.Extensions.Configuration; | |||
global using Microsoft.Extensions.DependencyInjection; | |||
global using Microsoft.Extensions.Logging; | |||
global using Microsoft.Extensions.Options; | |||
global using System.Collections.Generic; | |||
global using System.Linq; | |||
global using System.Net.Http; | |||
global using System.Net; | |||
global using System.Text.Json; | |||
global using System.Threading.Tasks; | |||
global using System; | |||
global using Services.Common; |
@ -1,27 +1,13 @@ | |||
global using System; | |||
global using System.Collections.Generic; | |||
global using System.Linq; | |||
global using System.Net; | |||
global using System.Net.Http; | |||
global using System.Net.Http.Headers; | |||
global using System.Text.Json; | |||
global using System.Threading; | |||
global using System.Threading.Tasks; | |||
global using System.Text.Json; | |||
global using CatalogApi; | |||
global using Grpc.Core; | |||
global using Grpc.Core.Interceptors; | |||
global using GrpcBasket; | |||
global using Microsoft.AspNetCore.Authentication; | |||
global using Microsoft.AspNetCore.Authorization; | |||
global using Microsoft.AspNetCore.Builder; | |||
global using Microsoft.AspNetCore.Http; | |||
global using Microsoft.AspNetCore.Mvc; | |||
global using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Config; | |||
global using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Infrastructure; | |||
global using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Models; | |||
global using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services; | |||
global using Microsoft.Extensions.Configuration; | |||
global using Microsoft.Extensions.DependencyInjection; | |||
global using Microsoft.Extensions.Logging; | |||
global using Microsoft.Extensions.Options; | |||
global using Services.Common; |
@ -1,8 +1,4 @@ | |||
global using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; | |||
global using System.Text.Json.Serialization; | |||
global using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; | |||
global using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; | |||
global using static Microsoft.eShopOnContainers.BuildingBlocks.EventBus.InMemoryEventBusSubscriptionsManager; | |||
global using System.Collections.Generic; | |||
global using System.Linq; | |||
global using System.Text.Json.Serialization; | |||
global using System.Threading.Tasks; | |||
global using System; |
@ -1,16 +1,13 @@ | |||
global using Microsoft.Extensions.Logging; | |||
global using System.Net.Sockets; | |||
global using System.Text; | |||
global using System.Text.Json; | |||
global using Microsoft.eShopOnContainers.BuildingBlocks.EventBus; | |||
global using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; | |||
global using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; | |||
global using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Extensions; | |||
global using Microsoft.Extensions.Logging; | |||
global using Polly; | |||
global using Polly.Retry; | |||
global using RabbitMQ.Client; | |||
global using RabbitMQ.Client.Events; | |||
global using RabbitMQ.Client.Exceptions; | |||
global using System; | |||
global using System.IO; | |||
global using System.Net.Sockets; | |||
global using Microsoft.eShopOnContainers.BuildingBlocks.EventBus; | |||
global using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; | |||
global using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; | |||
global using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Extensions; | |||
global using System.Text; | |||
global using System.Threading.Tasks; | |||
global using System.Text.Json; |
@ -1,12 +1,8 @@ | |||
global using Microsoft.EntityFrameworkCore; | |||
global using Microsoft.EntityFrameworkCore.Metadata.Builders; | |||
global using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; | |||
global using System; | |||
global using System.Text.Json; | |||
global using System.ComponentModel.DataAnnotations.Schema; | |||
global using System.Linq; | |||
global using System.Threading.Tasks; | |||
global using Microsoft.EntityFrameworkCore.Storage; | |||
global using System.Collections.Generic; | |||
global using System.ComponentModel.DataAnnotations.Schema; | |||
global using System.Data.Common; | |||
global using System.Reflection; | |||
global using System.Text.Json; | |||
global using Microsoft.EntityFrameworkCore; | |||
global using Microsoft.EntityFrameworkCore.Metadata.Builders; | |||
global using Microsoft.EntityFrameworkCore.Storage; | |||
global using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; |
@ -1,77 +1,75 @@ | |||
using System; | |||
using System.Data.SqlClient; | |||
using System.Data.SqlClient; | |||
using Microsoft.EntityFrameworkCore; | |||
using Microsoft.Extensions.Configuration; | |||
using Microsoft.Extensions.DependencyInjection; | |||
using Microsoft.Extensions.Logging; | |||
using Polly; | |||
namespace Microsoft.AspNetCore.Hosting | |||
namespace Microsoft.AspNetCore.Hosting; | |||
public static class IWebHostExtensions | |||
{ | |||
public static class IWebHostExtensions | |||
public static bool IsInKubernetes(this IServiceProvider services) | |||
{ | |||
public static bool IsInKubernetes(this IServiceProvider services) | |||
{ | |||
var cfg = services.GetService<IConfiguration>(); | |||
var orchestratorType = cfg.GetValue<string>("OrchestratorType"); | |||
return orchestratorType?.ToUpper() == "K8S"; | |||
} | |||
public static IServiceProvider MigrateDbContext<TContext>(this IServiceProvider services, Action<TContext, IServiceProvider> seeder) where TContext : DbContext | |||
{ | |||
var underK8s = services.IsInKubernetes(); | |||
using var scope = services.CreateScope(); | |||
var scopeServices = scope.ServiceProvider; | |||
var logger = scopeServices.GetRequiredService<ILogger<TContext>>(); | |||
var context = scopeServices.GetService<TContext>(); | |||
var cfg = services.GetService<IConfiguration>(); | |||
var orchestratorType = cfg.GetValue<string>("OrchestratorType"); | |||
return orchestratorType?.ToUpper() == "K8S"; | |||
} | |||
try | |||
{ | |||
logger.LogInformation("Migrating database associated with context {DbContextName}", typeof(TContext).Name); | |||
public static IServiceProvider MigrateDbContext<TContext>(this IServiceProvider services, Action<TContext, IServiceProvider> seeder) where TContext : DbContext | |||
{ | |||
var underK8s = services.IsInKubernetes(); | |||
if (underK8s) | |||
{ | |||
InvokeSeeder(seeder, context, scopeServices); | |||
} | |||
else | |||
{ | |||
var retries = 10; | |||
var retry = Policy.Handle<SqlException>() | |||
.WaitAndRetry( | |||
retryCount: retries, | |||
sleepDurationProvider: retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), | |||
onRetry: (exception, timeSpan, retry, ctx) => | |||
{ | |||
logger.LogWarning(exception, "[{prefix}] Error migrating database (attempt {retry} of {retries})", nameof(TContext), retry, retries); | |||
}); | |||
using var scope = services.CreateScope(); | |||
var scopeServices = scope.ServiceProvider; | |||
var logger = scopeServices.GetRequiredService<ILogger<TContext>>(); | |||
var context = scopeServices.GetService<TContext>(); | |||
//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, scopeServices)); | |||
} | |||
try | |||
{ | |||
logger.LogInformation("Migrating database associated with context {DbContextName}", typeof(TContext).Name); | |||
logger.LogInformation("Migrated database associated with context {DbContextName}", typeof(TContext).Name); | |||
if (underK8s) | |||
{ | |||
InvokeSeeder(seeder, context, scopeServices); | |||
} | |||
catch (Exception ex) | |||
else | |||
{ | |||
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 | |||
} | |||
var retries = 10; | |||
var retry = Policy.Handle<SqlException>() | |||
.WaitAndRetry( | |||
retryCount: retries, | |||
sleepDurationProvider: retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), | |||
onRetry: (exception, timeSpan, retry, ctx) => | |||
{ | |||
logger.LogWarning(exception, "[{prefix}] Error migrating database (attempt {retry} of {retries})", nameof(TContext), retry, retries); | |||
}); | |||
//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, scopeServices)); | |||
} | |||
return services; | |||
logger.LogInformation("Migrated database associated with context {DbContextName}", typeof(TContext).Name); | |||
} | |||
private static void InvokeSeeder<TContext>(Action<TContext, IServiceProvider> seeder, TContext context, IServiceProvider services) | |||
where TContext : DbContext | |||
catch (Exception ex) | |||
{ | |||
context.Database.Migrate(); | |||
seeder(context, services); | |||
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<TContext>(Action<TContext, IServiceProvider> seeder, TContext context, IServiceProvider services) | |||
where TContext : DbContext | |||
{ | |||
context.Database.Migrate(); | |||
seeder(context, services); | |||
} | |||
} |
@ -1,10 +1,8 @@ | |||
global using System; | |||
global using Microsoft.AspNetCore.Builder; | |||
global using Microsoft.AspNetCore.Builder; | |||
global using Microsoft.Extensions.Configuration; | |||
global using Microsoft.Extensions.DependencyInjection; | |||
global using Microsoft.Extensions.Hosting; | |||
global using Microsoft.Extensions.Logging; | |||
global using Ordering.BackgroundTasks; | |||
global using Ordering.BackgroundTasks.Extensions; | |||
global using Ordering.BackgroundTasks.Services; | |||
global using Services.Common; |
@ -1,12 +1,7 @@ | |||
global using global::Microsoft.eShopOnContainers.Services.Ordering.Domain.Exceptions; | |||
global using System.Reflection; | |||
global using global::Microsoft.eShopOnContainers.Services.Ordering.Domain.Exceptions; | |||
global using MediatR; | |||
global using Microsoft.eShopOnContainers.Services.Ordering.Domain.Seedwork; | |||
global using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate; | |||
global using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; | |||
global using Microsoft.eShopOnContainers.Services.Ordering.Domain.Events; | |||
global using System.Collections.Generic; | |||
global using System.Linq; | |||
global using System.Reflection; | |||
global using System.Threading.Tasks; | |||
global using System.Threading; | |||
global using System; | |||
global using Microsoft.eShopOnContainers.Services.Ordering.Domain.Seedwork; |
@ -1,17 +1,12 @@ | |||
global using MediatR; | |||
global using System.Data; | |||
global using MediatR; | |||
global using Microsoft.EntityFrameworkCore; | |||
global using Microsoft.EntityFrameworkCore.Design; | |||
global using Microsoft.EntityFrameworkCore.Metadata.Builders; | |||
global using Microsoft.EntityFrameworkCore.Storage; | |||
global using Microsoft.EntityFrameworkCore; | |||
global using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate; | |||
global using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; | |||
global using Microsoft.eShopOnContainers.Services.Ordering.Domain.Seedwork; | |||
global using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency; | |||
global using Microsoft.eShopOnContainers.Services.Ordering.Domain.Exceptions; | |||
global using Microsoft.eShopOnContainers.Services.Ordering.Domain.Seedwork; | |||
global using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.EntityConfigurations; | |||
global using System.Data; | |||
global using System.Linq; | |||
global using System.Threading.Tasks; | |||
global using System.Threading; | |||
global using System; | |||
global using System.Collections.Generic; | |||
global using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency; |
@ -1,11 +1,7 @@ | |||
global using System.Threading.Tasks; | |||
global using Microsoft.AspNetCore.Builder; | |||
global using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; | |||
global using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; | |||
global using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; | |||
global using Microsoft.eShopOnContainers.Payment.API; | |||
global using Microsoft.eShopOnContainers.Payment.API.IntegrationEvents.EventHandling; | |||
global using Microsoft.eShopOnContainers.Payment.API.IntegrationEvents.Events; | |||
global using Microsoft.Extensions.DependencyInjection; | |||
global using Microsoft.Extensions.Logging; | |||
global using Microsoft.Extensions.Options; | |||
global using Services.Common; |
@ -1,14 +1,14 @@ | |||
global using System; | |||
global using System.Collections.Generic; | |||
global using System.Linq; | |||
global using System.Net.Http; | |||
global using Microsoft.AspNetCore.Http; | |||
global using System.Security.Claims; | |||
global using System.Text; | |||
global using System.Text.Json; | |||
global using System.Threading.Tasks; | |||
global using Microsoft.AspNetCore.Http; | |||
global using Microsoft.eShopOnContainers.Services.Basket.API.Model; | |||
global using Microsoft.eShopOnContainers.WebMVC.ViewModels; | |||
global using System.Text.Json; | |||
global using System.Collections.Generic; | |||
global using System.Linq; | |||
global using System.Text; | |||
global using WebMVC.Services.ModelDTOs; | |||
global using Xunit; | |||
@ -1,13 +1,5 @@ | |||
global using Microsoft.AspNetCore.Mvc; | |||
global using Microsoft.Extensions.Configuration; | |||
global using System.Linq; | |||
global using Microsoft.AspNetCore.Hosting; | |||
global using System; | |||
global using System.Collections.Generic; | |||
global using Azure.Core; | |||
global using Azure.Identity; | |||
global using Azure.Core; | |||
global using Microsoft.AspNetCore.Builder; | |||
global using Microsoft.AspNetCore.Diagnostics.HealthChecks; | |||
global using Microsoft.Extensions.DependencyInjection; | |||
global using Microsoft.AspNetCore.Mvc; | |||
global using Microsoft.Extensions.Diagnostics.HealthChecks; | |||
global using Microsoft.Extensions.Hosting; |