Microsoft.Data.SqlClient raised a timeout after 32 seconds when connecting to Azure SQL server

john john Pter 1,015 Reputation points
2025-04-09T22:53:04.4333333+00:00

I have a timely Azure Function wrote in .net core 8, and it uses Microsoft.Data.SqlClient, to connect to Azure SQL DB, here is a sample of the code:-

The main method:-

public async Task Run([TimerTrigger("0 0 5 * * *")] TimerInfo myTimer) //

{

try {

_logger.LogInformation($"Azure Function executed at: {DateTime.UtcNow}");

await UpsertDailyQualificationMilestoneToSqlServer(dailyQualificationMilestones);

_logger.LogInformation("Daily Qualification Milestone successfully processed.");

}

catch (Exception ex)

{

_logger.LogError($"Error: {ex.Message}");

}}

the Method to interact with SQL:-

private async Task UpsertDailyQualificationMilestoneToSqlServer(List<Milestone> Milestones)

{

using (IDbConnection conn = new SqlConnection(connectionString))

{

conn.Open();

foreach (var ar in Milestones)

{

string sql = @"

MERGE INTO DailyQualificationMilestone AS target

USING (SELECT @Id AS Id, @LogDate AS LogDate) AS source

ON target.Id = source.Id AND target.LogDate = source.LogDate

WHEN MATCHED THEN

UPDATE SET

Id = @Id,

LogDate = @LogDate,

LogType = @LogType,

LogActor = @LogActor,

Milestone = @Milestone,

Modified = GETDATE()

WHEN NOT MATCHED THEN

INSERT (Id, LogDate, LogType, LogActor, Milestone, Created, Modified)

VALUES (@Id, @LogDate, @LogType, @LogActor, @Milestone, GETDATE(), GETDATE());";

await conn.ExecuteAsync(sql, ar);

}

}

}

here is the connection string, which mentioned that the timeout is 120 seconds:-

Server=tcp:**.windows.net,1433;Authentication=Active Directory Managed Identity;Initial Catalog=*;Persist Security Info=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=120;

but yesterday when the azure function runs at 5 am UTC, it raised this exception:-

Error: Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

after 32 seconds when the azure function started?? so what get timed out exactly ? as the timeout should happen after 120 seconds and not after 32 seconds. here the azure function logs:-

User's image

so the azure function started at 5:00:02 am , but timed out at 5:00:35.. so what is going on? the Milestones passed to the function contained around 15K items to be inserted or updated inside SQL..

Thanks

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,430 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 74,956 Reputation points
    2025-04-09T23:09:21.0266667+00:00

    your error is query timeout, not connection.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.