Browse Source

Review MigrateDbContext to add retry for network-related exception when sql server container is created

pull/396/head
Unai Zorrilla Castro 7 years ago
parent
commit
8b264e0767
2 changed files with 22 additions and 2 deletions
  1. +1
    -0
      src/BuildingBlocks/WebHostCustomization/WebHost.Customization/WebHost.Customization.csproj
  2. +21
    -2
      src/BuildingBlocks/WebHostCustomization/WebHost.Customization/WebHostExtensions.cs

+ 1
- 0
src/BuildingBlocks/WebHostCustomization/WebHost.Customization/WebHost.Customization.csproj View File

@ -6,6 +6,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="Polly" Version="5.3.1" />
</ItemGroup>
</Project>

+ 21
- 2
src/BuildingBlocks/WebHostCustomization/WebHost.Customization/WebHostExtensions.cs View File

@ -1,7 +1,10 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Polly;
using Polly.Retry;
using System;
using System.Data.SqlClient;
namespace Microsoft.AspNetCore.Hosting
{
@ -21,10 +24,26 @@ namespace Microsoft.AspNetCore.Hosting
{
logger.LogInformation($"Migrating database associated with context {typeof(TContext).Name}");
context.Database
var retry = Policy.Handle<SqlException>()
.WaitAndRetry(new TimeSpan[]
{
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10),
TimeSpan.FromSeconds(15),
});
retry.Execute(() =>
{
//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.
context.Database
.Migrate();
seeder(context,services);
seeder(context, services);
});
logger.LogInformation($"Migrated database associated with context {typeof(TContext).Name}");
}


Loading…
Cancel
Save