MVC的异步,Entity Framework的异步,ADO.NET的异步,

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())

        {

            //...

        }

    }

}

复制代码

 

原文地址:http://www.cnblogs.com/cmt/p/aspnet_async_await.html

你可能感兴趣的:(framework)