SendMailAsync:异步模块或处理程序在异步操作仍处于挂起状态时完成 [英] SendMailAsync : An asynchronous module or handler completed while an asynchronous operation was still pending

查看:198
本文介绍了SendMailAsync:异步模块或处理程序在异步操作仍处于挂起状态时完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用SendMailAsync时,出现以下错误:

While using SendMailAsync I am getting the following error:

异步完成的异步模块或处理程序 操作仍在等待中

An asynchronous module or handler completed while an asynchronous operation was still pending

我的代码:

public static async Task SendEmail(MessageContent messageContent, string emailBody)
{
   SmtpClient smtpClientNoSend = new SmtpClient();
   await smtpClientNoSend.SendMailAsync(mailMessage);
}

从控制器呼叫:

public async System.Threading.Tasks.Task<ActionResult> Register()
{
   await SendEmail();
}

private void SendEmail()
{
  SMTPEmail.SendEmail(msg, output.ToString());
  return null;
}

推荐答案

您的呼叫层次结构已损坏.您不应该使用async void,这仅适用于事件处理程序,而应使用async Task:

Your call hierarchy is broken. You shouldn't use async void, that is ment for event handlers only, use async Task instead:

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

旁注-我不确定为什么会有这么多的SendMail方法,您可以将它们缩小到单个方法调用之内.

Side note - I'm not sure why you have so many SendMail methods, You could narrow them down to a single method call.

这篇关于SendMailAsync:异步模块或处理程序在异步操作仍处于挂起状态时完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆