Skip to content

Commit b1b7ca5

Browse files
Merge pull request dotnet-architecture#122 from dotnet/bugs/dotnet-architecture#75-NoRetryLoginAtCatch
Removed the catch with retry logic from CatalogContextSeed
2 parents 5432936 + 8a054ed commit b1b7ca5

2 files changed

Lines changed: 47 additions & 36 deletions

File tree

src/Services/Catalog/Catalog.API/Infrastructure/CatalogContextSeed.cs

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,47 +13,33 @@ public class CatalogContextSeed
1313
{
1414
public static async Task SeedAsync(IApplicationBuilder applicationBuilder, ILoggerFactory loggerFactory, int? retry = 0)
1515
{
16-
int retryForAvaiability = retry.Value;
17-
try
18-
{
19-
var context = (CatalogContext)applicationBuilder
20-
.ApplicationServices.GetService(typeof(CatalogContext));
21-
22-
context.Database.Migrate();
16+
var context = (CatalogContext)applicationBuilder
17+
.ApplicationServices.GetService(typeof(CatalogContext));
2318

24-
if (!context.CatalogBrands.Any())
25-
{
26-
context.CatalogBrands.AddRange(
27-
GetPreconfiguredCatalogBrands());
19+
context.Database.Migrate();
2820

29-
await context.SaveChangesAsync();
30-
}
31-
32-
if (!context.CatalogTypes.Any())
33-
{
34-
context.CatalogTypes.AddRange(
35-
GetPreconfiguredCatalogTypes());
21+
if (!context.CatalogBrands.Any())
22+
{
23+
context.CatalogBrands.AddRange(
24+
GetPreconfiguredCatalogBrands());
3625

37-
await context.SaveChangesAsync();
38-
}
26+
await context.SaveChangesAsync();
27+
}
3928

40-
if (!context.CatalogItems.Any())
41-
{
42-
context.CatalogItems.AddRange(
43-
GetPreconfiguredItems());
29+
if (!context.CatalogTypes.Any())
30+
{
31+
context.CatalogTypes.AddRange(
32+
GetPreconfiguredCatalogTypes());
4433

45-
await context.SaveChangesAsync();
46-
}
34+
await context.SaveChangesAsync();
4735
}
48-
catch (Exception ex)
36+
37+
if (!context.CatalogItems.Any())
4938
{
50-
if (retryForAvaiability < 10)
51-
{
52-
retryForAvaiability++;
53-
var log = loggerFactory.CreateLogger("catalog seed");
54-
log.LogError(ex.Message);
55-
await SeedAsync(applicationBuilder, loggerFactory, retryForAvaiability);
56-
}
39+
context.CatalogItems.AddRange(
40+
GetPreconfiguredItems());
41+
42+
await context.SaveChangesAsync();
5743
}
5844
}
5945

src/Services/Catalog/Catalog.API/Startup.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Microsoft.Extensions.DependencyInjection;
1212
using Microsoft.Extensions.Logging;
1313
using System;
14+
using System.Data.SqlClient;
1415
using System.IO;
1516
using System.Reflection;
1617
using System.Threading;
@@ -95,11 +96,35 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
9596
app.UseSwagger()
9697
.UseSwaggerUi();
9798

99+
var context = (CatalogContext)app
100+
.ApplicationServices.GetService(typeof(CatalogContext));
101+
102+
WaitForSqlAvailability(context, loggerFactory);
98103
//Seed Data
99104
CatalogContextSeed.SeedAsync(app, loggerFactory)
100-
.Wait();
105+
.Wait();
106+
}
101107

102-
108+
private void WaitForSqlAvailability(CatalogContext ctx, ILoggerFactory loggerFactory, int? retry = 0)
109+
{
110+
int retryForAvailability = retry.Value;
111+
try
112+
{
113+
ctx.Database.OpenConnection();
114+
}
115+
catch(SqlException ex)
116+
{
117+
if (retryForAvailability < 10)
118+
{
119+
retryForAvailability++;
120+
var log = loggerFactory.CreateLogger(nameof(Startup));
121+
log.LogError(ex.Message);
122+
WaitForSqlAvailability(ctx, loggerFactory, retryForAvailability);
123+
}
124+
}
125+
finally {
126+
ctx.Database.CloseConnection();
127+
}
103128
}
104129
}
105130
}

0 commit comments

Comments
 (0)