【发布时间】:2015-11-10 15:47:24
【问题描述】:
我对 ASP.NET MVC 的 async/await 非常陌生,并且有一个关于它的快速问题。我的问题是有必要在对数据库的嵌套调用中使用 async/await 吗?例如:
UnitOfWork.cs
public async Task CommitAsync()
{
await _context.SaveChangesAsync();
}
AccountService.cs
public async Task SaveLoginAttemptAsync(LoginAttempt loginAttempt)
{
_appContext.Repository<LoginAttempt>().Add(loginAttempt);
await _appContext.UnitOfWork.CommitAsync();
}
AccountController.cs
//..
var attempt = new LoginAttempt()
{
Email = User.Identity.GetEmail(),
Logintime = DateTime.Now,
Successful = successful ? "Y" : "N",
IpAddress = Request.UserHostAddress,
UserId = Convert.ToInt32(User.Identity.GetUserId())
};
await _accountService.SaveLoginAttemptAsync(attempt);
//..
由于端点使用异步/等待,AccountService.cs 中的 SaveLoginAttemptAsync 和 AccountController.cs 中的代码块是否需要异步/等待方法?
感谢您的帮助!
【问题讨论】:
标签: c# asp.net asp.net-mvc asynchronous async-await