Connection Resiliency

Connection resiliency is used to describe a scenario where some resource becomes unavailable for short period of time, if we try to connect that resource like SQL Server during that time our connection should not throw exception instead it should retry.

We can make a strategy for retries by exponentially increasing the wait time for next retry attempt. We also need to set up a cap on maximum retries.

Connection resiliency is specially handy for cloud resources like Azure SQL Database if it goes down for a brief period of time.

Entity Framework Core provides out of the box support for connection resiliency for Azure SQL Database.
However we need to enable and configure the strategy for each DbContext connection as shown below:

public class Startup
    public IServiceProvider ConfigureServices(IServiceCollection services)
        services.AddDbContext<ApplicationDbContext>(options =>
            sqlServerOptionsAction: sqlOptions =>
            maxRetryCount: 5,
            maxRetryDelay: TimeSpan.FromSeconds(30),
            errorNumbersToAdd: null);
Share it