【发布时间】:2019-01-22 16:29:26
【问题描述】:
我使用 await 功能来发送电子邮件。在等待等待功能完成时,是否有任何解决方案如何显示页面或返回视图()。
这是我的代码:
using (var smtp = new SmtpClient())
{
var credential = new NetworkCredential
{
UserName = "email@gmail.com",
Password = "paswordEmail"
};
smtp.Credentials = credential;
smtp.Host = "smtp-mail.outlook.com";
smtp.Port = 587;
smtp.EnableSsl = true;
await smtp.SendMailAsync(message); //Here is my await function for sending an email
return RedirectToAction("ThankYou"); //This thank you page. I want to display this html page without waiting for await complete
}
【问题讨论】:
-
只是不要等待发送。
-
如果不等待发送,则将其从 using 块中删除,否则它可能会在有时间完成之前超出范围。
-
@AlexanderHiggins 如果删除等待则出错 - [InvalidOperationException:异步模块或处理程序已完成,而异步操作仍处于挂起状态。]
-
如果您需要等待,请将其包装在 Task.Run(()=> {});
-
您也可以在 Task.Run(()=> {}) 中调用 send 或使用发布的 Async Nkosi
标签: c# asp.net-mvc async-await