Skip to content

Commit 373bdd9

Browse files
Set Retries with exponential backoff to most microservices sqlOptions, so errors when spinning the SQL container late shouldn't happen.
1 parent 99686fa commit 373bdd9

4 files changed

Lines changed: 31 additions & 19 deletions

File tree

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
7070
{
7171
sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
7272
//Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
73-
sqlOptions.EnableRetryOnFailure(maxRetryCount: 5, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
73+
sqlOptions.EnableRetryOnFailure(maxRetryCount: 10, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
7474
});
7575

7676
// Changing default behavior when client evaluation occurs to throw.
@@ -79,13 +79,6 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
7979
//Check Client vs. Server evaluation: https://docs.microsoft.com/en-us/ef/core/querying/client-eval
8080
});
8181

82-
services.AddDbContext<IntegrationEventLogContext>(options =>
83-
{
84-
options.UseSqlServer(Configuration["ConnectionString"], opts =>
85-
opts.MigrationsAssembly("Catalog.API"));
86-
});
87-
88-
8982
services.Configure<CatalogSettings>(Configuration);
9083

9184
// Add framework services.

src/Services/Identity/Identity.API/Startup.cs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
3333
{
3434
// Add framework services.
3535
services.AddDbContext<ApplicationDbContext>(options =>
36-
options.UseSqlServer(Configuration["ConnectionString"]));
36+
options.UseSqlServer(Configuration["ConnectionString"],
37+
sqlServerOptionsAction: sqlOptions =>
38+
{
39+
sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
40+
//Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
41+
sqlOptions.EnableRetryOnFailure(maxRetryCount: 10, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
42+
}));
3743

3844
services.AddIdentity<ApplicationUser, IdentityRole>()
3945
.AddEntityFrameworkStores<ApplicationDbContext>()
@@ -77,13 +83,23 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
7783
.AddAspNetIdentity<ApplicationUser>()
7884
.AddConfigurationStore(options =>
7985
{
80-
options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString, opts =>
81-
opts.MigrationsAssembly(migrationsAssembly));
86+
options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString,
87+
sqlServerOptionsAction: sqlOptions =>
88+
{
89+
sqlOptions.MigrationsAssembly(migrationsAssembly);
90+
//Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
91+
sqlOptions.EnableRetryOnFailure(maxRetryCount: 10, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
92+
});
8293
})
8394
.AddOperationalStore(options =>
8495
{
85-
options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString, opts =>
86-
opts.MigrationsAssembly(migrationsAssembly));
96+
options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString,
97+
sqlServerOptionsAction: sqlOptions =>
98+
{
99+
sqlOptions.MigrationsAssembly(migrationsAssembly);
100+
//Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
101+
sqlOptions.EnableRetryOnFailure(maxRetryCount: 10, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
102+
});
87103
})
88104
.Services.AddTransient<IProfileService, ProfileService>();
89105

src/Services/Marketing/Marketing.API/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
7373
{
7474
sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
7575
//Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
76-
sqlOptions.EnableRetryOnFailure(maxRetryCount: 5, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
76+
sqlOptions.EnableRetryOnFailure(maxRetryCount: 10, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
7777
});
7878

7979
// Changing default behavior when client evaluation occurs to throw.

src/Services/Ordering/Ordering.API/Startup.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,21 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
7575
sqlServerOptionsAction: sqlOptions =>
7676
{
7777
sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
78-
sqlOptions.EnableRetryOnFailure(maxRetryCount: 5, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
78+
sqlOptions.EnableRetryOnFailure(maxRetryCount: 10, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
7979
});
8080
},
8181
ServiceLifetime.Scoped //Showing explicitly that the DbContext is shared across the HTTP request scope (graph of objects started in the HTTP request)
8282
);
8383

8484
services.AddDbContext<IntegrationEventLogContext>(options =>
8585
{
86-
options.UseSqlServer(Configuration["ConnectionString"], opts =>
87-
{
88-
opts.MigrationsAssembly("Ordering.API");
89-
});
86+
options.UseSqlServer(Configuration["ConnectionString"],
87+
sqlServerOptionsAction: sqlOptions =>
88+
{
89+
sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
90+
//Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
91+
sqlOptions.EnableRetryOnFailure(maxRetryCount: 10, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
92+
});
9093
});
9194

9295

0 commit comments

Comments
 (0)