【发布时间】:2014-06-08 19:55:01
【问题描述】:
下面的方法有什么区别吗?一个比另一个更可取吗?
public static async Task SendAsync1(string to, string subject, string htmlBody) {
// ...
await smtp.SendMailAsync(message);
// No return statement
}
public static Task SendAsync2(string to, string subject, string htmlBody) {
// ...
return smtp.SendMailAsync(message);
}
这个方法将从 MVC 控制器方法中调用;例如:
public async Task<ActionResult> RegisterUser(RegisterViewModel model)
{
// ...
await Mailer.SendAsync(user.Email, subject, body);
return View(model);
}
【问题讨论】:
-
如果使用 SendAsync1,可能会在 2 个地方维护状态(通过 async/await 状态机制)的成本,但也许 async/await 状态机足够聪明,可以优化它...只是一个想法,但它很有趣。
标签: c# .net task-parallel-library async-await