While using SendMailAsync
I am getting the following error:
在使用SendMailAsync时,我得到了以下错误:
An asynchronous module or handler completed while an asynchronous operation was still pending
在异步操作仍处于挂起状态时完成的异步模块或处理程序
My code :
我的代码:
public static async Task SendEmail(MessageContent messageContent, string emailBody)
{
SmtpClient smtpClientNoSend = new SmtpClient();
await smtpClientNoSend.SendMailAsync(mailMessage);
}
Call From Controller:
电话控制器:
public async System.Threading.Tasks.Task<ActionResult> Register()
{
await SendEmail();
}
private void SendEmail()
{
SMTPEmail.SendEmail(msg, output.ToString());
return null;
}
1 个解决方案
#1
19
Your call hierarchy is broken. You shouldn't use async void
, that is ment for event handlers only, use async Task
instead:
您的调用层次结构被破坏了。不应使用async void,即仅用于事件处理程序,而应使用async任务:
public static Task SendEmailAsync(MessageContent messageContent, string emailBody)
{
SmtpClient smtpClientNoSend = new SmtpClient();
return smtpClientNoSend.SendMailAsync(mailMessage);
}
public async Task<ActionResult> Register()
{
await SendEmailAsync();
}
private Task SendEmailAsync()
{
return SMTPEmail.SendEmailAsync(msg, output.ToString());
}
Side note - I'm not sure why you have so many SendMail
methods, You could narrow them down to a single method call.
边注-我不知道为什么您有这么多的SendMail方法,您可以将它们缩小到一个单一的方法调用。
#1
19
Your call hierarchy is broken. You shouldn't use async void
, that is ment for event handlers only, use async Task
instead:
您的调用层次结构被破坏了。不应使用async void,即仅用于事件处理程序,而应使用async任务:
public static Task SendEmailAsync(MessageContent messageContent, string emailBody)
{
SmtpClient smtpClientNoSend = new SmtpClient();
return smtpClientNoSend.SendMailAsync(mailMessage);
}
public async Task<ActionResult> Register()
{
await SendEmailAsync();
}
private Task SendEmailAsync()
{
return SMTPEmail.SendEmailAsync(msg, output.ToString());
}
Side note - I'm not sure why you have so many SendMail
methods, You could narrow them down to a single method call.
边注-我不知道为什么您有这么多的SendMail方法,您可以将它们缩小到一个单一的方法调用。