1717 using Microsoft . Extensions . HealthChecks ;
1818 using Microsoft . Extensions . Logging ;
1919 using Microsoft . Extensions . Options ;
20+ using Polly ;
2021 using RabbitMQ . Client ;
2122 using System ;
2223 using System . Data . Common ;
2324 using System . Data . SqlClient ;
2425 using System . Reflection ;
26+ using System . Threading . Tasks ;
2527
2628 public class Startup
2729 {
@@ -85,7 +87,7 @@ public void ConfigureServices(IServiceCollection services)
8587 services . AddSwaggerGen ( options =>
8688 {
8789 options . DescribeAllEnumsAsStrings ( ) ;
88- options . SwaggerDoc ( "v1" , new Swashbuckle . AspNetCore . Swagger . Info
90+ options . SwaggerDoc ( "v1" , new Swashbuckle . AspNetCore . Swagger . Info
8991 {
9092 Title = "eShopOnContainers - Catalog HTTP API" ,
9193 Version = "v1" ,
@@ -144,11 +146,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
144146 var context = ( CatalogContext ) app
145147 . ApplicationServices . GetService ( typeof ( CatalogContext ) ) ;
146148
147- WaitForSqlAvailability ( context , loggerFactory ) ;
148-
149- //Seed Data
150- CatalogContextSeed . SeedAsync ( app , loggerFactory )
151- . Wait ( ) ;
149+ WaitForSqlAvailabilityAsync ( context , loggerFactory , app ) . Wait ( ) ;
152150
153151 var integrationEventLogContext = new IntegrationEventLogContext (
154152 new DbContextOptionsBuilder < IntegrationEventLogContext > ( )
@@ -158,28 +156,28 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
158156 integrationEventLogContext . Database . Migrate ( ) ;
159157 }
160158
161- private void WaitForSqlAvailability ( CatalogContext ctx , ILoggerFactory loggerFactory , int ? retry = 0 )
159+ private async Task WaitForSqlAvailabilityAsync ( CatalogContext ctx , ILoggerFactory loggerFactory , IApplicationBuilder app , int retries = 0 )
162160 {
163- int retryForAvailability = retry . Value ;
164-
165- try
166- {
167- ctx . Database . OpenConnection ( ) ;
168- }
169- catch ( SqlException ex )
170- {
171- if ( retryForAvailability < 10 )
172- {
173- retryForAvailability ++ ;
174- var log = loggerFactory . CreateLogger ( nameof ( Startup ) ) ;
175- log . LogError ( ex . Message ) ;
176- WaitForSqlAvailability ( ctx , loggerFactory , retryForAvailability ) ;
177- }
178- }
179- finally
161+ var logger = loggerFactory . CreateLogger ( nameof ( Startup ) ) ;
162+ var policy = CreatePolicy ( retries , logger , nameof ( WaitForSqlAvailabilityAsync ) ) ;
163+ await policy . ExecuteAsync ( async ( ) =>
180164 {
181- ctx . Database . CloseConnection ( ) ;
182- }
165+ await CatalogContextSeed . SeedAsync ( app , loggerFactory ) ;
166+ } ) ;
167+
168+ }
169+
170+ private Policy CreatePolicy ( int retries , ILogger logger , string prefix )
171+ {
172+ return Policy . Handle < SqlException > ( ) .
173+ WaitAndRetryAsync (
174+ retryCount : retries ,
175+ sleepDurationProvider : retry => TimeSpan . FromSeconds ( 5 ) ,
176+ onRetry : ( exception , timeSpan , retry , ctx ) =>
177+ {
178+ logger . LogTrace ( $ "[{ prefix } ] Exception { exception . GetType ( ) . Name } with message ${ exception . Message } detected on attempt { retry } of { retries } ") ;
179+ }
180+ ) ;
183181 }
184182 }
185183}
0 commit comments