Merge pull request #122 from dotnet/bugs/#75-NoRetryLoginAtCatch
Removed the catch with retry logic from CatalogContextSeed
This commit is contained in:
commit
b1b7ca5a4c
@ -13,47 +13,33 @@
|
|||||||
{
|
{
|
||||||
public static async Task SeedAsync(IApplicationBuilder applicationBuilder, ILoggerFactory loggerFactory, int? retry = 0)
|
public static async Task SeedAsync(IApplicationBuilder applicationBuilder, ILoggerFactory loggerFactory, int? retry = 0)
|
||||||
{
|
{
|
||||||
int retryForAvaiability = retry.Value;
|
var context = (CatalogContext)applicationBuilder
|
||||||
try
|
.ApplicationServices.GetService(typeof(CatalogContext));
|
||||||
|
|
||||||
|
context.Database.Migrate();
|
||||||
|
|
||||||
|
if (!context.CatalogBrands.Any())
|
||||||
{
|
{
|
||||||
var context = (CatalogContext)applicationBuilder
|
context.CatalogBrands.AddRange(
|
||||||
.ApplicationServices.GetService(typeof(CatalogContext));
|
GetPreconfiguredCatalogBrands());
|
||||||
|
|
||||||
context.Database.Migrate();
|
await context.SaveChangesAsync();
|
||||||
|
|
||||||
if (!context.CatalogBrands.Any())
|
|
||||||
{
|
|
||||||
context.CatalogBrands.AddRange(
|
|
||||||
GetPreconfiguredCatalogBrands());
|
|
||||||
|
|
||||||
await context.SaveChangesAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!context.CatalogTypes.Any())
|
|
||||||
{
|
|
||||||
context.CatalogTypes.AddRange(
|
|
||||||
GetPreconfiguredCatalogTypes());
|
|
||||||
|
|
||||||
await context.SaveChangesAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!context.CatalogItems.Any())
|
|
||||||
{
|
|
||||||
context.CatalogItems.AddRange(
|
|
||||||
GetPreconfiguredItems());
|
|
||||||
|
|
||||||
await context.SaveChangesAsync();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
|
if (!context.CatalogTypes.Any())
|
||||||
{
|
{
|
||||||
if (retryForAvaiability < 10)
|
context.CatalogTypes.AddRange(
|
||||||
{
|
GetPreconfiguredCatalogTypes());
|
||||||
retryForAvaiability++;
|
|
||||||
var log = loggerFactory.CreateLogger("catalog seed");
|
await context.SaveChangesAsync();
|
||||||
log.LogError(ex.Message);
|
}
|
||||||
await SeedAsync(applicationBuilder, loggerFactory, retryForAvaiability);
|
|
||||||
}
|
if (!context.CatalogItems.Any())
|
||||||
|
{
|
||||||
|
context.CatalogItems.AddRange(
|
||||||
|
GetPreconfiguredItems());
|
||||||
|
|
||||||
|
await context.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Data.SqlClient;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
@ -95,11 +96,35 @@
|
|||||||
app.UseSwagger()
|
app.UseSwagger()
|
||||||
.UseSwaggerUi();
|
.UseSwaggerUi();
|
||||||
|
|
||||||
|
var context = (CatalogContext)app
|
||||||
|
.ApplicationServices.GetService(typeof(CatalogContext));
|
||||||
|
|
||||||
|
WaitForSqlAvailability(context, loggerFactory);
|
||||||
//Seed Data
|
//Seed Data
|
||||||
CatalogContextSeed.SeedAsync(app, loggerFactory)
|
CatalogContextSeed.SeedAsync(app, loggerFactory)
|
||||||
.Wait();
|
.Wait();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void WaitForSqlAvailability(CatalogContext ctx, ILoggerFactory loggerFactory, int? retry = 0)
|
||||||
|
{
|
||||||
|
int retryForAvailability = retry.Value;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ctx.Database.OpenConnection();
|
||||||
|
}
|
||||||
|
catch(SqlException ex)
|
||||||
|
{
|
||||||
|
if (retryForAvailability < 10)
|
||||||
|
{
|
||||||
|
retryForAvailability++;
|
||||||
|
var log = loggerFactory.CreateLogger(nameof(Startup));
|
||||||
|
log.LogError(ex.Message);
|
||||||
|
WaitForSqlAvailability(ctx, loggerFactory, retryForAvailability);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
ctx.Database.CloseConnection();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user