From 075d592722bd47df00756e03c0f0d1986011f7a2 Mon Sep 17 00:00:00 2001 From: Italo Cotta Date: Mon, 30 Dec 2024 22:10:37 +0100 Subject: [PATCH] Add CancellationToken support to QueryAsync overload This change introduces a CancellationToken parameter to the QueryAsync method, enabling support for cooperative cancellation of asynchronous database queries. The modification ensures better resilience and control over long-running or potentially cancelable operations. This fixes issues #2125 and #1938. --- Dapper/PublicAPI.Shipped.txt | 2 +- Dapper/SqlMapper.Async.cs | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Dapper/PublicAPI.Shipped.txt b/Dapper/PublicAPI.Shipped.txt index c1b08ea99..284c08b71 100644 --- a/Dapper/PublicAPI.Shipped.txt +++ b/Dapper/PublicAPI.Shipped.txt @@ -249,7 +249,7 @@ static Dapper.SqlMapper.QueryAsync(th static Dapper.SqlMapper.QueryAsync(this System.Data.IDbConnection! cnn, string! sql, System.Func! map, object? param = null, System.Data.IDbTransaction? transaction = null, bool buffered = true, string! splitOn = "Id", int? commandTimeout = null, System.Data.CommandType? commandType = null) -> System.Threading.Tasks.Task!>! static Dapper.SqlMapper.QueryAsync(this System.Data.IDbConnection! cnn, Dapper.CommandDefinition command, System.Func! map, string! splitOn = "Id") -> System.Threading.Tasks.Task!>! static Dapper.SqlMapper.QueryAsync(this System.Data.IDbConnection! cnn, string! sql, System.Func! map, object? param = null, System.Data.IDbTransaction? transaction = null, bool buffered = true, string! splitOn = "Id", int? commandTimeout = null, System.Data.CommandType? commandType = null) -> System.Threading.Tasks.Task!>! -static Dapper.SqlMapper.QueryAsync(this System.Data.IDbConnection! cnn, string! sql, System.Type![]! types, System.Func! map, object? param = null, System.Data.IDbTransaction? transaction = null, bool buffered = true, string! splitOn = "Id", int? commandTimeout = null, System.Data.CommandType? commandType = null) -> System.Threading.Tasks.Task!>! +static Dapper.SqlMapper.QueryAsync(this System.Data.IDbConnection! cnn, string! sql, System.Type![]! types, System.Func! map, object? param = null, System.Data.IDbTransaction? transaction = null, bool buffered = true, string! splitOn = "Id", int? commandTimeout = null, System.Data.CommandType? commandType = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task!>! static Dapper.SqlMapper.QueryCachePurged -> System.EventHandler? static Dapper.SqlMapper.QueryFirst(this System.Data.IDbConnection! cnn, string! sql, object? param = null, System.Data.IDbTransaction? transaction = null, int? commandTimeout = null, System.Data.CommandType? commandType = null) -> dynamic! static Dapper.SqlMapper.QueryFirst(this System.Data.IDbConnection! cnn, System.Type! type, string! sql, object? param = null, System.Data.IDbTransaction? transaction = null, int? commandTimeout = null, System.Data.CommandType? commandType = null) -> object! diff --git a/Dapper/SqlMapper.Async.cs b/Dapper/SqlMapper.Async.cs index 9408d5735..933f36c37 100644 --- a/Dapper/SqlMapper.Async.cs +++ b/Dapper/SqlMapper.Async.cs @@ -963,11 +963,12 @@ private static async Task> MultiMapAsyncThe field we should split and read the second object from (default: "Id"). /// Number of seconds before command execution timeout. /// Is it a stored proc or a batch? + /// The cancellation token for this command. /// An enumerable of . [System.Diagnostics.CodeAnalysis.SuppressMessage("ApiDesign", "RS0026:Do not add multiple public overloads with optional parameters", Justification = "Grandfathered")] - public static Task> QueryAsync(this IDbConnection cnn, string sql, Type[] types, Func map, object? param = null, IDbTransaction? transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null, CommandType? commandType = null) + public static Task> QueryAsync(this IDbConnection cnn, string sql, Type[] types, Func map, object? param = null, IDbTransaction? transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null, CommandType? commandType = null, CancellationToken cancellationToken = default) { - var command = new CommandDefinition(sql, param, transaction, commandTimeout, commandType, buffered ? CommandFlags.Buffered : CommandFlags.None, default); + var command = new CommandDefinition(sql, param, transaction, commandTimeout, commandType, buffered ? CommandFlags.Buffered : CommandFlags.None, cancellationToken); return MultiMapAsync(cnn, command, types, map, splitOn); }