最佳答案
我有下一个方法:
public async Task<IEnumerable<Quote>> GetQuotesAsync()
{
using var connection = new SqlConnection(_connectionString);
var allQuotes = await connection.QueryAsync<Quote>(@"SELECT [Symbol], [Bid], [Ask], [Digits] FROM [QuoteEngine].[RealtimeData]");
return allQuotes;
}
一切都很好,清晰,连接将在范围结束处置。
但 resharper 建议将其改为:
public async Task<IEnumerable<Quote>> GetQuotesAsync()
{
await using var connection = new SqlConnection(_connectionString);
var allQuotes = await connection.QueryAsync<Quote>(@"SELECT [Symbol], [Bid], [Ask], [Digits] FROM [QuoteEngine].[RealtimeData]");
return allQuotes;
}
它在使用之前添加了等待,并且成功地编译了代码。这意味着什么? 我们什么时候需要这么做?