Dapper.NETで非同期APIを使用すると、拡張メソッドで渡すコマンドのタイムアウト値が尊重されないことに気付きました。 その後、私はSqlCommand.CommandTimeoutのMSDN documentationを見つけました。そして、これは「サポートされていない」と思われます。Dapper.NET非同期APIを使用するときCommandTimeoutを尊重する方法
CommandTimeoutプロパティは、BeginExecuteReaderなどの非同期メソッド 呼び出し中は無視されます。
私は基本クラスで次のメソッドを使用しています。
public async Task<int> ExecuteAsync(string sql, object param = null,
CommandType commandType = CommandType.Text, int? commandTimeout = null, IDbTransaction transaction = null)
{
using (var connection = Connection)
{
Task<int> queryTask = connection.ExecuteAsync(sql, param, transaction, commandTimeout ?? CommandTimeoutDefault, commandType);
int result = await queryTask.ConfigureAwait(false);
connection.Close();
connection.Dispose();
return result;
}
}
public async Task<IEnumerable<TEntity>> QueryAsync(string sql, object param = null,
CommandType commandType = CommandType.Text, int? commandTimeout = null, IDbTransaction transaction = null)
{
using (var connection = Connection)
{
Task<IEnumerable<TEntity>> queryTask = connection.QueryAsync<TEntity>(sql, param, transaction, commandTimeout ?? CommandTimeoutDefault, commandType);
IEnumerable<TEntity> data = await queryTask.ConfigureAwait(false);
connection.Close();
connection.Dispose();
return data;
}
}
セイCommandTimeoutDefault
は、私が50秒を取って、要求がまだ評価されますことを、SEができ、30です。
非同期Dapper.NET APIを使用してタイムアウト間隔で接続を切断して処理する方法はありますか?
マイナー無関係なもの: Close/Disposeが必要です - 既存のコードはすでに 'using 'を介してそれらのことを行うでしょう –