Azure Function app returns transient error when accessing SQL Server database

lqfhib0f  于 11个月前  发布在  SQL Server
关注(0)|答案(1)|浏览(119)

I am trying to access a database (hosted in Azure) from a function app (hosted within the same Azure account).

I get the following error:
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.

If I add EnableRetryOnFailure , it eventually returns with a timeout.

I have the following in my program.cs file:

string connectionString = Environment.GetEnvironmentVariable("DefaultConnection");

services.AddDbContext<VladbContext>(options => options.UseSqlServer(connectionString), ServiceLifetime.Transient);

We are using .NET 7 and Entity Framework Core. The EF Core code is shared with a Blazor app that works perfectly fine. The function also works fine on my local computer (connected to local database).

What could be the problem?

Update--------------------

Here is the error from the logs
"Exception: System.AggregateException: One or more errors occurred. (Retry failed after 4 tries. Retry settings can be adjusted in ClientOptions.Retry or by configuring a custom retry policy in ClientOptions.RetryPolicy. (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (ourserver.vault.azure.net:443)) (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (ourserver.vault.azure.net:443)) (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (ourserver.vault.azure.net:443)) (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (ourserver.vault.azure.net:443)))"

Update 2------------

We have now enabled SQL server to access keyvault (no idea why it needed to do that but hey)

We now get the following error in the logs instead.
2023-12-28T14:26:58.830 [Error] An error occurred using the connection to database 'ourSQLdB' on server 'tcp:ourSQLserver.database.windows.net,1433'. 2023-12-28T14:26:58.892 [Error] An exception occurred while iterating over the results of a query for context type 'App.DataService.Models.VladbContext'. System.InvalidOperationException: 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. ---> Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0 - A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.) ---> System.ComponentModel.Win32Exception (10060): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)

91zkwejq

91zkwejq1#

Microsoft's recommended ServiceLifetime for a DbContext is scoped .

There really shouldn't be any reason to use transient for the lifetime as it can cause a multitude of issues, especially if you're attempting to call the transient dbcontext from a longer-lived scoped lifetime service.

The built-in DI container will track all created instances of DbContext which, at least in the past, caused memory leaks and unexpected behavior when the actual root scope is long-lived.

EDIT: You might want to check your query to make sure that you're not ending up with a dead-lock or with a very under-performant query that may be causing the issues with SQL Server.

I suggest you review your Query Performance Insights to check for any Resource Consuming or Long Runnign queries that may be causing issues. Also, you might want to try scaling up your database temporarily to see if that solves the issue.

相关问题