MVC的异步化改造
无比轻松,只要把ActionResult改为async Task<AstionResult>:
public async Task<ActionResult> SiteHome(int? pageIndex) { //... }
Entity Framework的异步化
也很轻松,查询时只需使用异步LINQ:
public async Task<int> GetAsync() { return await Entities .Where(...) .Select(...) .CountAsync(); }
保存时只需SaveChangesAsync():
async Task IUnitOfWork.CommitAsync() { await base.SaveChangesAsync(); }
ADO.NET的异步化
所有进行异步化的数据库操作都需要用类似下面的ADO.NET代码进行改造
using(var conn = new SqlConnection(connectionString)) { using(var command = conn.CreateCommand()) { command.CommandType = CommandType.StoredProcedure; command.CommandText = "..."; command.Parameters.AddWithValue("...", ...); await conn.OpenAsync(); using (IDataReader reader = await command.ExecuteReaderAsync()) { //... } } }