C#C
C#3y ago
pirrippu

✅ SQL Command Timeout does not work with async calls

Has anyone encountered the following issue? When using a sync call to the database, the command timeout parameter works, but when I'm using an async call it does not work?

See sample code below:
var connection = new MySqlConnection("***");await connection.OpenAsync();
var command = new MySqlCommand("select Count(Id) from eventlog", connection);
command.CommandTimeout = 10;

// Hits the exception at 10 second mark
var count = command.ExecuteScalar();

// Executes indefinitely
var count = await command.ExecuteScalarAsync(cancellationToken);


Removing the CommandTimeout, meaning using the default timeout of 30 seconds, works for sync calls but not with async calls.

I tried adding a connection timeout and it also does not work. I tried using Dapper, and it also does not work. Am I missing something or what?
Was this page helpful?