Closed dbakuntsev closed 6 years ago
AsEnumerable
doesn't rely on any external dependencies, so it doesn't need an async version.
I thought about making async versions of ForEach
as they are currently written, but it would call back to user code, which could be blocking on other things, and I think people would be more likely to shoot themselves with deadlocks. (And it was pretty icky when I added them back in .NET 3.0)
With modern c#, you can just do:
var items = await connection.QuerySqlAsync<>(...);
items.ForEach(i => await action(i));
Alternatively, I could make ForEachAsync
use ToListAsync
instead of AsEnumerable
, and that would make it somewhat safe, but I'm not sure that it buys you much.
Do you have a use case where this would be helpful?
Thank you for the response!
AsEnumerable
actually does have an external dependency on ReadAsync
. That's the call I want to abort with the CancellationToken
, I'm not as concerned with the loop itself.
The use case is simple, really. It happens all the time in ETL scenarios.
foreach (var item in connection.AsEnumerable<>(...))
on a query that takes a while to return the first result.As currently implemented, the thread is blocked retrieving the first entry from the enumerable, there's no way to cancel that wait but for the server to start returning results, or for the command to time out.
ah yes, that makes sense. Let's see what we can do about that.
The code in the 6.2.4 branch has an AsEnumerableAsync
method. I made ToListAsync
use it, so it should be working. It checks the cancellation token before it reads the result schema and also passes the token to the Read and NextResult calls. I think it should make things cancellable for you.
Give it a shot. I'll be doing an official build in the next day or two.
These are now available in v6.2.4
Type
What kind of issue is this?
Current Behavior
"Async" versions of ForEach, ForEachSql, and AsEnumerable extensions are missing. It would be very helpful to have an optional CancellationToken parameter in these functions, too!