@@ -373,19 +373,49 @@ private static Task<DbDataReader> ExecuteReaderWithFlagsFallbackAsync(DbCommand
373
373
return task ;
374
374
}
375
375
376
+ /// <summary>
377
+ /// Attempts to open a connection asynchronously, with a better error message for unsupported usages.
378
+ /// </summary>
379
+ private static Task TryOpenAsync ( this IDbConnection cnn , CancellationToken cancel )
380
+ {
381
+ if ( cnn is DbConnection dbConn )
382
+ {
383
+ return dbConn . OpenAsync ( cancel ) ;
384
+ }
385
+ else
386
+ {
387
+ throw new InvalidOperationException ( "Async operations require use of a DbConnection or an already-open IDbConnection" ) ;
388
+ }
389
+ }
390
+
391
+ /// <summary>
392
+ /// Attempts setup a <see cref="DbCommand"/> on a <see cref="DbConnection"/>, with a better error message for unsupported usages.
393
+ /// </summary>
394
+ private static DbCommand TrySetupAsyncCommand ( this CommandDefinition command , IDbConnection cnn , Action < IDbCommand , object > paramReader )
395
+ {
396
+ if ( command . SetupCommand ( cnn , paramReader ) is DbCommand dbCommand )
397
+ {
398
+ return dbCommand ;
399
+ }
400
+ else
401
+ {
402
+ throw new InvalidOperationException ( "Async operations require use of a DbConnection or an IDbConnection where .CreateCommand() returns a DbCommand" ) ;
403
+ }
404
+ }
405
+
376
406
private static async Task < IEnumerable < T > > QueryAsync < T > ( this IDbConnection cnn , Type effectiveType , CommandDefinition command )
377
407
{
378
408
object param = command . Parameters ;
379
409
var identity = new Identity ( command . CommandText , command . CommandType , cnn , effectiveType , param ? . GetType ( ) , null ) ;
380
410
var info = GetCacheInfo ( identity , param , command . AddToCache ) ;
381
411
bool wasClosed = cnn . State == ConnectionState . Closed ;
382
412
var cancel = command . CancellationToken ;
383
- using ( var cmd = ( DbCommand ) command . SetupCommand ( cnn , info . ParamReader ) )
413
+ using ( var cmd = command . TrySetupAsyncCommand ( cnn , info . ParamReader ) )
384
414
{
385
415
DbDataReader reader = null ;
386
416
try
387
417
{
388
- if ( wasClosed ) await ( ( DbConnection ) cnn ) . OpenAsync ( cancel ) . ConfigureAwait ( false ) ;
418
+ if ( wasClosed ) await cnn . TryOpenAsync ( cancel ) . ConfigureAwait ( false ) ;
389
419
reader = await ExecuteReaderWithFlagsFallbackAsync ( cmd , wasClosed , CommandBehavior . SequentialAccess | CommandBehavior . SingleResult , cancel ) . ConfigureAwait ( false ) ;
390
420
391
421
var tuple = info . Deserializer ;
@@ -444,12 +474,12 @@ private static async Task<T> QueryRowAsync<T>(this IDbConnection cnn, Row row, T
444
474
var info = GetCacheInfo ( identity , param , command . AddToCache ) ;
445
475
bool wasClosed = cnn . State == ConnectionState . Closed ;
446
476
var cancel = command . CancellationToken ;
447
- using ( var cmd = ( DbCommand ) command . SetupCommand ( cnn , info . ParamReader ) )
477
+ using ( var cmd = command . TrySetupAsyncCommand ( cnn , info . ParamReader ) )
448
478
{
449
479
DbDataReader reader = null ;
450
480
try
451
481
{
452
- if ( wasClosed ) await ( ( DbConnection ) cnn ) . OpenAsync ( cancel ) . ConfigureAwait ( false ) ;
482
+ if ( wasClosed ) await cnn . TryOpenAsync ( cancel ) . ConfigureAwait ( false ) ;
453
483
reader = await ExecuteReaderWithFlagsFallbackAsync ( cmd , wasClosed , ( row & Row . Single ) != 0
454
484
? CommandBehavior . SequentialAccess | CommandBehavior . SingleResult // need to allow multiple rows, to check fail condition
455
485
: CommandBehavior . SequentialAccess | CommandBehavior . SingleResult | CommandBehavior . SingleRow , cancel ) . ConfigureAwait ( false ) ;
@@ -546,7 +576,7 @@ private static async Task<int> ExecuteMultiImplAsync(IDbConnection cnn, CommandD
546
576
bool wasClosed = cnn . State == ConnectionState . Closed ;
547
577
try
548
578
{
549
- if ( wasClosed ) await ( ( DbConnection ) cnn ) . OpenAsync ( command . CancellationToken ) . ConfigureAwait ( false ) ;
579
+ if ( wasClosed ) await cnn . TryOpenAsync ( command . CancellationToken ) . ConfigureAwait ( false ) ;
550
580
551
581
CacheInfo info = null ;
552
582
string masterSql = null ;
@@ -562,7 +592,7 @@ private static async Task<int> ExecuteMultiImplAsync(IDbConnection cnn, CommandD
562
592
if ( isFirst )
563
593
{
564
594
isFirst = false ;
565
- cmd = ( DbCommand ) command . SetupCommand ( cnn , null ) ;
595
+ cmd = command . TrySetupAsyncCommand ( cnn , null ) ;
566
596
masterSql = cmd . CommandText ;
567
597
var identity = new Identity ( command . CommandText , cmd . CommandType , cnn , null , obj . GetType ( ) , null ) ;
568
598
info = GetCacheInfo ( identity , obj , command . AddToCache ) ;
@@ -577,7 +607,7 @@ private static async Task<int> ExecuteMultiImplAsync(IDbConnection cnn, CommandD
577
607
}
578
608
else
579
609
{
580
- cmd = ( DbCommand ) command . SetupCommand ( cnn , null ) ;
610
+ cmd = command . TrySetupAsyncCommand ( cnn , null ) ;
581
611
}
582
612
info . ParamReader ( cmd , obj ) ;
583
613
@@ -604,7 +634,7 @@ private static async Task<int> ExecuteMultiImplAsync(IDbConnection cnn, CommandD
604
634
}
605
635
else
606
636
{
607
- using ( var cmd = ( DbCommand ) command . SetupCommand ( cnn , null ) )
637
+ using ( var cmd = command . TrySetupAsyncCommand ( cnn , null ) )
608
638
{
609
639
foreach ( var obj in multiExec )
610
640
{
@@ -640,11 +670,11 @@ private static async Task<int> ExecuteImplAsync(IDbConnection cnn, CommandDefini
640
670
var identity = new Identity ( command . CommandText , command . CommandType , cnn , null , param ? . GetType ( ) , null ) ;
641
671
var info = GetCacheInfo ( identity , param , command . AddToCache ) ;
642
672
bool wasClosed = cnn . State == ConnectionState . Closed ;
643
- using ( var cmd = ( DbCommand ) command . SetupCommand ( cnn , info . ParamReader ) )
673
+ using ( var cmd = command . TrySetupAsyncCommand ( cnn , info . ParamReader ) )
644
674
{
645
675
try
646
676
{
647
- if ( wasClosed ) await ( ( DbConnection ) cnn ) . OpenAsync ( command . CancellationToken ) . ConfigureAwait ( false ) ;
677
+ if ( wasClosed ) await cnn . TryOpenAsync ( command . CancellationToken ) . ConfigureAwait ( false ) ;
648
678
var result = await cmd . ExecuteNonQueryAsync ( command . CancellationToken ) . ConfigureAwait ( false ) ;
649
679
command . OnCompleted ( ) ;
650
680
return result ;
@@ -910,8 +940,8 @@ private static async Task<IEnumerable<TReturn>> MultiMapAsync<TFirst, TSecond, T
910
940
bool wasClosed = cnn . State == ConnectionState . Closed ;
911
941
try
912
942
{
913
- if ( wasClosed ) await ( ( DbConnection ) cnn ) . OpenAsync ( command . CancellationToken ) . ConfigureAwait ( false ) ;
914
- using ( var cmd = ( DbCommand ) command . SetupCommand ( cnn , info . ParamReader ) )
943
+ if ( wasClosed ) await cnn . TryOpenAsync ( command . CancellationToken ) . ConfigureAwait ( false ) ;
944
+ using ( var cmd = command . TrySetupAsyncCommand ( cnn , info . ParamReader ) )
915
945
using ( var reader = await ExecuteReaderWithFlagsFallbackAsync ( cmd , wasClosed , CommandBehavior . SequentialAccess | CommandBehavior . SingleResult , command . CancellationToken ) . ConfigureAwait ( false ) )
916
946
{
917
947
if ( ! command . Buffered ) wasClosed = false ; // handing back open reader; rely on command-behavior
@@ -960,8 +990,8 @@ private static async Task<IEnumerable<TReturn>> MultiMapAsync<TReturn>(this IDbC
960
990
bool wasClosed = cnn . State == ConnectionState . Closed ;
961
991
try
962
992
{
963
- if ( wasClosed ) await ( ( DbConnection ) cnn ) . OpenAsync ( ) . ConfigureAwait ( false ) ;
964
- using ( var cmd = ( DbCommand ) command . SetupCommand ( cnn , info . ParamReader ) )
993
+ if ( wasClosed ) await cnn . TryOpenAsync ( command . CancellationToken ) . ConfigureAwait ( false ) ;
994
+ using ( var cmd = command . TrySetupAsyncCommand ( cnn , info . ParamReader ) )
965
995
using ( var reader = await ExecuteReaderWithFlagsFallbackAsync ( cmd , wasClosed , CommandBehavior . SequentialAccess | CommandBehavior . SingleResult , command . CancellationToken ) . ConfigureAwait ( false ) )
966
996
{
967
997
var results = MultiMapImpl ( null , default ( CommandDefinition ) , types , map , splitOn , reader , identity , true ) ;
@@ -1015,8 +1045,8 @@ public static async Task<GridReader> QueryMultipleAsync(this IDbConnection cnn,
1015
1045
bool wasClosed = cnn . State == ConnectionState . Closed ;
1016
1046
try
1017
1047
{
1018
- if ( wasClosed ) await ( ( DbConnection ) cnn ) . OpenAsync ( command . CancellationToken ) . ConfigureAwait ( false ) ;
1019
- cmd = ( DbCommand ) command . SetupCommand ( cnn , info . ParamReader ) ;
1048
+ if ( wasClosed ) await cnn . TryOpenAsync ( command . CancellationToken ) . ConfigureAwait ( false ) ;
1049
+ cmd = command . TrySetupAsyncCommand ( cnn , info . ParamReader ) ;
1020
1050
reader = await ExecuteReaderWithFlagsFallbackAsync ( cmd , wasClosed , CommandBehavior . SequentialAccess , command . CancellationToken ) . ConfigureAwait ( false ) ;
1021
1051
1022
1052
var result = new GridReader ( cmd , reader , identity , command . Parameters as DynamicParameters , command . AddToCache , command . CancellationToken ) ;
@@ -1108,8 +1138,8 @@ private static async Task<IDataReader> ExecuteReaderImplAsync(IDbConnection cnn,
1108
1138
bool wasClosed = cnn . State == ConnectionState . Closed ;
1109
1139
try
1110
1140
{
1111
- cmd = ( DbCommand ) command . SetupCommand ( cnn , paramReader ) ;
1112
- if ( wasClosed ) await ( ( DbConnection ) cnn ) . OpenAsync ( command . CancellationToken ) . ConfigureAwait ( false ) ;
1141
+ cmd = command . TrySetupAsyncCommand ( cnn , paramReader ) ;
1142
+ if ( wasClosed ) await cnn . TryOpenAsync ( command . CancellationToken ) . ConfigureAwait ( false ) ;
1113
1143
var reader = await ExecuteReaderWithFlagsFallbackAsync ( cmd , wasClosed , commandBehavior , command . CancellationToken ) . ConfigureAwait ( false ) ;
1114
1144
wasClosed = false ;
1115
1145
return reader ;
@@ -1182,8 +1212,8 @@ private static async Task<T> ExecuteScalarImplAsync<T>(IDbConnection cnn, Comman
1182
1212
object result ;
1183
1213
try
1184
1214
{
1185
- cmd = ( DbCommand ) command . SetupCommand ( cnn , paramReader ) ;
1186
- if ( wasClosed ) await ( ( DbConnection ) cnn ) . OpenAsync ( command . CancellationToken ) . ConfigureAwait ( false ) ;
1215
+ cmd = command . TrySetupAsyncCommand ( cnn , paramReader ) ;
1216
+ if ( wasClosed ) await cnn . TryOpenAsync ( command . CancellationToken ) . ConfigureAwait ( false ) ;
1187
1217
result = await cmd . ExecuteScalarAsync ( command . CancellationToken ) . ConfigureAwait ( false ) ;
1188
1218
command . OnCompleted ( ) ;
1189
1219
}
0 commit comments