Getting error while calling Azure SQL database from dockerized ASP.NET Core Web API project

Viewed 46

I get this exception:

An exception has been raised that is likely due to a transient failure. Consider enabling transient error resiliency by adding 'EnableRetryOnFailure' to the 'UseSqlServer' call

while calling the API from a dockerized .NET Core project.

Working fine while executing normally through Visual Studio.

I tried whitelisting Ip addresses with this on Azure click to see

This is my connection string

Server=tcp:tweetapp-server.database.windows.net,1433;Initial Catalog=tweetappapi;User ID=admin;Password={password};Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;

1 Answers

Network connection problems, the temporary unavailability of a service, timeout problems, etc. can all result in problems. The error indicates how to handle this, and the setup for our DbContext, as shown below, includes EnableRetryOnFailure.

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<PicnicContext>(
        options => options.UseSqlServer(
            "<connection string>",
            providerOptions => providerOptions.EnableRetryOnFailure()));
}

EnableRetryOnFailure() may also be used to set options like the maximum retry count, the maximum retry delay, the number of errors to add, etc.

The initial request of the app will take longer than usual when the aforementioned parameters are configured and the connection has been established, but the user won't see an error because the database will be reachable after one or two connect retries.

Related