fix identity server
This commit is contained in:
parent
f8c42c2fbe
commit
1671062715
79
src/Services/Identity/Identity.API/IWebHostExtensions.cs
Normal file
79
src/Services/Identity/Identity.API/IWebHostExtensions.cs
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Polly;
|
||||||
|
using System;
|
||||||
|
using System.Data.SqlClient;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNetCore.Hosting
|
||||||
|
{
|
||||||
|
public static class IWebHostExtensions
|
||||||
|
{
|
||||||
|
public static bool IsInKubernetes(this IWebHost webHost)
|
||||||
|
{
|
||||||
|
var cfg = webHost.Services.GetService<IConfiguration>();
|
||||||
|
var orchestratorType = cfg.GetValue<string>("OrchestratorType");
|
||||||
|
return orchestratorType?.ToUpper() == "K8S";
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IWebHost MigrateDbContext<TContext>(this IWebHost webHost, Action<TContext, IServiceProvider> seeder) where TContext : DbContext
|
||||||
|
{
|
||||||
|
var underK8s = webHost.IsInKubernetes();
|
||||||
|
|
||||||
|
using (var scope = webHost.Services.CreateScope())
|
||||||
|
{
|
||||||
|
var services = scope.ServiceProvider;
|
||||||
|
var logger = services.GetRequiredService<ILogger<TContext>>();
|
||||||
|
var context = services.GetService<TContext>();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
logger.LogInformation("Migrating database associated with context {DbContextName}", typeof(TContext).Name);
|
||||||
|
|
||||||
|
if (underK8s)
|
||||||
|
{
|
||||||
|
InvokeSeeder(seeder, context, services);
|
||||||
|
}
|
||||||
|
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}] Exception {ExceptionType} with message {Message} detected on attempt {retry} of {retries}", nameof(TContext), exception.GetType().Name, exception.Message, 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, services));
|
||||||
|
}
|
||||||
|
|
||||||
|
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 webHost;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void InvokeSeeder<TContext>(Action<TContext, IServiceProvider> seeder, TContext context, IServiceProvider services)
|
||||||
|
where TContext : DbContext
|
||||||
|
{
|
||||||
|
context.Database.Migrate();
|
||||||
|
seeder(context, services);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -19,6 +19,8 @@
|
|||||||
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="$(AspNetCore_HealthChecks_SqlServer)" />
|
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="$(AspNetCore_HealthChecks_SqlServer)" />
|
||||||
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="$(AspNetCore_HealthChecks_UI_Client)" />
|
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="$(AspNetCore_HealthChecks_UI_Client)" />
|
||||||
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="$(Autofac_Extensions_DependencyInjection)" />
|
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="$(Autofac_Extensions_DependencyInjection)" />
|
||||||
|
<PackageReference Include="IdentityServer4" Version="3.0.0-preview3.4" />
|
||||||
|
<PackageReference Include="IdentityServer4.Storage" Version="3.0.0-preview3.4" />
|
||||||
<PackageReference Include="IdentityServer4.AspNetIdentity" Version="$(IdentityServer4_AspNetIdentity)" />
|
<PackageReference Include="IdentityServer4.AspNetIdentity" Version="$(IdentityServer4_AspNetIdentity)" />
|
||||||
<PackageReference Include="IdentityServer4.EntityFramework" Version="$(IdentityServer4_EntityFramework)" />
|
<PackageReference Include="IdentityServer4.EntityFramework" Version="$(IdentityServer4_EntityFramework)" />
|
||||||
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="$(Microsoft_ApplicationInsights_AspNetCore)" />
|
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="$(Microsoft_ApplicationInsights_AspNetCore)" />
|
||||||
@ -27,13 +29,14 @@
|
|||||||
<PackageReference Include="Microsoft.AspNetCore.DataProtection.Redis" Version="$(Microsoft_AspNetCore_DataProtection_Redis)" />
|
<PackageReference Include="Microsoft.AspNetCore.DataProtection.Redis" Version="$(Microsoft_AspNetCore_DataProtection_Redis)" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.HealthChecks" Version="$(Microsoft_AspNetCore_Diagnostics_HealthChecks)" />
|
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.HealthChecks" Version="$(Microsoft_AspNetCore_Diagnostics_HealthChecks)" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.HealthChecks" Version="$(Microsoft_AspNetCore_HealthChecks)" />
|
<PackageReference Include="Microsoft.AspNetCore.HealthChecks" Version="$(Microsoft_AspNetCore_HealthChecks)" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="$(Microsoft_EntityFrameworkCore_Design)">
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.0.0-preview5.19227.1">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration.AzureKeyVault" Version="$(Microsoft_Extensions_Configuration_AzureKeyVault)" />
|
<PackageReference Include="Microsoft.Extensions.Configuration.AzureKeyVault" Version="$(Microsoft_Extensions_Configuration_AzureKeyVault)" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.AzureAppServices" Version="$(Microsoft_Extensions_Logging_AzureAppServices)" />
|
<PackageReference Include="Microsoft.Extensions.Logging.AzureAppServices" Version="$(Microsoft_Extensions_Logging_AzureAppServices)" />
|
||||||
<PackageReference Include="Microsoft.Web.LibraryManager.Build" Version="$(Microsoft_Web_LibraryManager_Build)" />
|
<PackageReference Include="Microsoft.Web.LibraryManager.Build" Version="$(Microsoft_Web_LibraryManager_Build)" />
|
||||||
|
<PackageReference Include="Polly" Version="$(Polly)" />
|
||||||
<PackageReference Include="Serilog.AspNetCore" Version="$(Serilog_AspNetCore)" />
|
<PackageReference Include="Serilog.AspNetCore" Version="$(Serilog_AspNetCore)" />
|
||||||
<PackageReference Include="Serilog.Enrichers.Environment" Version="$(Serilog_Enrichers_Environment)" />
|
<PackageReference Include="Serilog.Enrichers.Environment" Version="$(Serilog_Enrichers_Environment)" />
|
||||||
<PackageReference Include="Serilog.Settings.Configuration" Version="$(Serilog_Settings_Configuration)" />
|
<PackageReference Include="Serilog.Settings.Configuration" Version="$(Serilog_Settings_Configuration)" />
|
||||||
@ -41,8 +44,8 @@
|
|||||||
<PackageReference Include="Serilog.Sinks.Seq" Version="$(Serilog_Sinks_Seq)" />
|
<PackageReference Include="Serilog.Sinks.Seq" Version="$(Serilog_Sinks_Seq)" />
|
||||||
<PackageReference Include="Serilog.Sinks.Http" Version="$(Serilog_Sinks_Http)" />
|
<PackageReference Include="Serilog.Sinks.Http" Version="$(Serilog_Sinks_Http)" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="$(Swashbuckle_AspNetCore)" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="$(Swashbuckle_AspNetCore)" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="$(Microsoft_AspNetCore_Identity_EntityFrameworkCore)" />
|
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.0.0-preview5-19227-01" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="$(Microsoft_AspNetCore_Diagnostics_EntityFrameworkCore)" />
|
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.0.0-preview5-19227-01" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<!--<Target Name="PrepublishScript" BeforeTargets="PrepareForPublish">
|
<!--<Target Name="PrepublishScript" BeforeTargets="PrepareForPublish">
|
||||||
@ -57,10 +60,6 @@
|
|||||||
<EmbeddedResource Include="Certificate\idsrv3test.pfx" />
|
<EmbeddedResource Include="Certificate\idsrv3test.pfx" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<ProjectReference Include="..\..\..\BuildingBlocks\WebHostCustomization\WebHost.Customization\WebHost.Customization.csproj" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Update="Setup\*">
|
<None Update="Setup\*">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user