使用 Dapper QueryAsync 方法时 connection.OpenAsync 和 connection.Open 之间的区别
Difference between connection.OpenAsync and connection.Open when using Dapper QueryAsync method
使用dapper的QueryAsync方法时使用connection.OpenAsync()和connection.Open()有什么区别
异步:
public async Task<IList<Product>> GetListAsync()
{
using (var connection = new SqlConnection(_connectionString))
{
StringBuilder sql = new StringBuilder();
sql.AppendLine("SELECT Id, Name ");
sql.AppendLine("FROM Product ");
await connection.OpenAsync();
var tickets = await connection.QueryAsync<Ticket>(sql.ToString());
return tickets.ToList();
}
}
非异步:
public async Task<IList<Product>> GetListAsync()
{
using (var connection = new SqlConnection(_connectionString))
{
StringBuilder sql = new StringBuilder();
sql.AppendLine("SELECT Id, Name ");
sql.AppendLine("FROM Product ");
connection.Open();
var tickets = await connection.QueryAsync<Ticket>(sql.ToString());
return tickets.ToList();
}
}
根据 source code 对于 Open
和 OpenAsync
对于 DbConnection
,
abstract public void Open();
public Task OpenAsync() {
return OpenAsync(CancellationToken.None);
}
public virtual Task OpenAsync(CancellationToken cancellationToken) {
TaskCompletionSource<object> taskCompletionSource = new TaskCompletionSource<object>();
if (cancellationToken.IsCancellationRequested) {
taskCompletionSource.SetCanceled();
}
else {
try {
Open();
taskCompletionSource.SetResult(null);
}
catch (Exception e) {
taskCompletionSource.SetException(e);
}
}
return taskCompletionSource.Task;
}
OpenAsync
只是允许在不阻塞线程的情况下等待异步代码。
现在有了 Dapper,您不必打开连接,因为如果连接关闭,该方法会在内部打开连接。
OpenAsync()
将 return 立即从方法 (GetListAsync
) 到调用它的方法,这样线程可以同时自由地做其他事情。例如,假设打开连接需要一秒钟(仅作为示例),然后线程可以做其他事情。连接的打开不会由调用它的线程完成。
连接打开后,执行将转到下一行:
var tickets = await connection.QueryAsync<Ticket>(sql.ToString());
注意:请注意,在某些情况下,如果IsCompleted
return为真,那么它将同步完成。
使用dapper的QueryAsync方法时使用connection.OpenAsync()和connection.Open()有什么区别
异步:
public async Task<IList<Product>> GetListAsync()
{
using (var connection = new SqlConnection(_connectionString))
{
StringBuilder sql = new StringBuilder();
sql.AppendLine("SELECT Id, Name ");
sql.AppendLine("FROM Product ");
await connection.OpenAsync();
var tickets = await connection.QueryAsync<Ticket>(sql.ToString());
return tickets.ToList();
}
}
非异步:
public async Task<IList<Product>> GetListAsync()
{
using (var connection = new SqlConnection(_connectionString))
{
StringBuilder sql = new StringBuilder();
sql.AppendLine("SELECT Id, Name ");
sql.AppendLine("FROM Product ");
connection.Open();
var tickets = await connection.QueryAsync<Ticket>(sql.ToString());
return tickets.ToList();
}
}
根据 source code 对于 Open
和 OpenAsync
对于 DbConnection
,
abstract public void Open();
public Task OpenAsync() {
return OpenAsync(CancellationToken.None);
}
public virtual Task OpenAsync(CancellationToken cancellationToken) {
TaskCompletionSource<object> taskCompletionSource = new TaskCompletionSource<object>();
if (cancellationToken.IsCancellationRequested) {
taskCompletionSource.SetCanceled();
}
else {
try {
Open();
taskCompletionSource.SetResult(null);
}
catch (Exception e) {
taskCompletionSource.SetException(e);
}
}
return taskCompletionSource.Task;
}
OpenAsync
只是允许在不阻塞线程的情况下等待异步代码。
现在有了 Dapper,您不必打开连接,因为如果连接关闭,该方法会在内部打开连接。
OpenAsync()
将 return 立即从方法 (GetListAsync
) 到调用它的方法,这样线程可以同时自由地做其他事情。例如,假设打开连接需要一秒钟(仅作为示例),然后线程可以做其他事情。连接的打开不会由调用它的线程完成。
连接打开后,执行将转到下一行:
var tickets = await connection.QueryAsync<Ticket>(sql.ToString());
注意:请注意,在某些情况下,如果IsCompleted
return为真,那么它将同步完成。