I have come upon a bit of code that looks like this (it is based of Dapper tutorial):
await using var con = new SqlConnection("some connection");
if (CancellationToken.IsCancellationRequested) await con.OpenAsync(CancellationToken);
return await con.QueryAsync(query, parameters);
How I am understanding this is if Cancellation token was set then it will open an async connection with that token. However if it is not set then it will not try to open a connection and instead it will just call the query statement.
What I'm trying to understand is if you don't call con.OpenAsync
will con.QueryAsync
call it? Essentially do I need to explicitly call it if there is no cancellation token?
What I'm trying to understand is if you don't call
con.OpenAsync
willcon.QueryAsync
call it?
Essentially do I need to explicitly call it if there is no cancellation token?
no; you don't need to call it with or without a cancellation token
However! if you are going to perform multiple related operations - perhaps involving a temporary table or transaction - then you will need to do your own connection state management, i.e. you will need to call Open[Async]
yourself (and Close
, if you aren't disposing it immediately).