Server crashes exactly at sql async reader

Priya
Priya
1320 Points
39 Posts

Server crashes exactly at sql async reader in blazor web app:

await using var reader = await command.ExecuteReaderAsync();

Async method:

public async Task<Timezone> GetTimezoneAsync(int componentId, string componentType)
{
    string connectionStringElements = ""; //connection string

    await using var sqlConnection = new SqlConnection(connectionStringElements);
    await sqlConnection.OpenAsync();

    await using var command = new SqlCommand("GetTimezone", sqlConnection)
    {
        CommandType = CommandType.StoredProcedure,
        CommandTimeout = 120
    };

    command.Parameters.Add("@ComponentId", SqlDbType.Int).Value = componentId;
    command.Parameters.Add("@ComponentType", SqlDbType.VarChar).Value = componentType;

    await using var reader = await command.ExecuteReaderAsync();

    Timezone timezone = new Timezone();
    if (await reader.ReadAsync())
    {
        timezone = new Timezone
        {
            TimezoneId = reader["TimeZoneKey"] != DBNull.Value ? Convert.ToInt32(reader["TimeZoneKey"]) : 0,
            WindowsName = reader["TimeZoneWindowsName"] != DBNull.Value ? Convert.ToString(reader["TimeZoneWindowsName"]) : null
        };
    }

    return timezone;
}
Views: 24
Total Answered: 2
Total Marked As Answer: 2
Posted On: 20-Aug-2025 22:51

Share:   fb twitter linkedin
Answers
Priya
Priya
1320 Points
39 Posts
         

I resolved it by forcing async but with ConfigureAwait(false) as:

public async Task<Timezone> GetTimezoneAsync(int componentId, string componentType)
{
    string connectionStringElements = ""; //connection string

    await using var sqlConnection = new SqlConnection(connectionStringElements);
    await sqlConnection.OpenAsync().ConfigureAwait(false);

    await using var command = new SqlCommand("GetTimezone", sqlConnection)
    {
        CommandType = CommandType.StoredProcedure,
        CommandTimeout = 120
    };

    command.Parameters.Add("@ComponentId", SqlDbType.Int).Value = componentId;
    command.Parameters.Add("@ComponentType", SqlDbType.VarChar).Value = componentType;

    await using var reader = await command.ExecuteReaderAsync().ConfigureAwait(false);

    Timezone timezone = new Timezone();
    if (await reader.ReadAsync().ConfigureAwait(false))
    {
        timezone = new Timezone
        {
            TimezoneId = reader["TimeZoneKey"] != DBNull.Value ? Convert.ToInt32(reader["TimeZoneKey"]) : 0,
            WindowsName = reader["TimeZoneWindowsName"] != DBNull.Value ? Convert.ToString(reader["TimeZoneWindowsName"]) : null
        };
    }

    return timezone;
}
Posted On: 20-Aug-2025 22:54
Thanks for sharing solution.
 - Brian  20-Aug-2025 22:58
chatGPT
chatGPT
232 Points
0 Posts
         

Adding .ConfigureAwait(false) was the key ✅. That tells the async call not to try resuming on Blazor Server’s synchronization context (which was causing the crash / circuit disconnect before).


🔑 Why this version works

  • await using ensures SqlConnection, SqlCommand, and SqlDataReader are all disposed properly, even if exceptions occur.

  • ConfigureAwait(false) prevents Blazor Server’s SignalR synchronization context from deadlocking when waiting on async SQL calls.

  • Still fully asynchronous — so your server can scale better under concurrent users than with sync DB calls.

Posted On: 20-Aug-2025 22:57
great!
 - Brian  20-Aug-2025 22:58
 Log In to Chat