我需要从异步操作发送异步电子邮件。我不明白为什么会发生以下错误,因为我在其他项目中使用同一个类并且只使用相同的表单而没有错误,一切都很安静。
错误:
异步操作方法'EsqueciMinhaSenhaAsync'返回一个Task 不能同步执行。
动作:
[AllowAnonymous]
[HttpPost, ValidateAntiForgeryToken]
public async Task<ActionResult> EsqueciMinhaSenhaAsync(UsuarioEsqueciMinhaSenhaViewModel vModel)
{
if (ModelState.IsValid)
{
var conteudo = "este é o conteudo do email";
var nomeRemetente = "esse é o nome do remetente";
if(await EmailService.SendAsync(Language.PasswordRecovery, conteudo, vModel.EmailOuUsername, nomeRemetente))
{
TempData["MensagemRetorno"] = Language.EmailSendedWithSuccess;
return View("login");
}
}
TempData["MensagemRetorno"] = Language.ErrorSendingEmail;
return View("EsqueciMinhaSenha");
}
我的电子邮件服务:
public static async Task<bool> SendAsync(string assunto, string conteudo, string destinatario, string nomeDestinatario)
{
// Habilitar o envio de e-mail
var appSetting = ConfigurationManager.AppSettings;
if (appSetting != null && appSetting.Count >= 7 && !string.IsNullOrEmpty(assunto) && !string.IsNullOrEmpty(conteudo) && !string.IsNullOrEmpty(destinatario) && !string.IsNullOrEmpty(nomeDestinatario))
{
int port = 0;
bool useSSl = false;
using (var msg = new MailMessage
{
From = new MailAddress(appSetting["EmailFrom"], appSetting["EmailNameFrom"]),
Body = WebUtility.HtmlEncode(conteudo)
})
{
int.TryParse(appSetting["EmailPort"], out port);
bool.TryParse(appSetting["EmailUseSSL"], out useSSl);
msg.ReplyToList.Add(destinatario);
msg.To.Add(new MailAddress(destinatario, nomeDestinatario));
msg.Subject = assunto;
msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(msg.Body, null, MediaTypeNames.Text.Plain));
msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(msg.Body, null, MediaTypeNames.Text.Html));
using (var smtpClient = new SmtpClient(appSetting["EmailServer"], port))
{
var credentials = new NetworkCredential(appSetting["EmailUserName"], appSetting["EmailPassword"]);
smtpClient.Credentials = credentials;
smtpClient.EnableSsl = useSSl;
await smtpClient.SendMailAsync(msg);
return await Task.FromResult(true);
}
}
}
return await Task.FromResult(false);
}
堆栈追踪:
[InvalidOperationException:异步操作方法 'esqueciminhasenha'返回一个无法执行的任务 同步]
System.Web.Mvc.Async.TaskAsyncActionDescriptor.Execute(ControllerContext controllerContext,IDictionary2 parameters) +119
2 参数)+27
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
System.Web.Mvc&LT;&GT; c__DisplayClass15.b__12() +55 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter) filter,ActionExecutingContext preContext,Func1 continuation) +253
1个过滤器,ActionDescriptor actionDescriptor, IDictionary的
System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +22 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList2 parameters) +189
2.CallEndDelegate(IAsyncResult的 asyncResult)+16
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +521
System.Web.Mvc.Controller.ExecuteCore() +95
MusiArtist.MVC.Areas.admin.Controllers.BaseController.ExecuteCore() in F:\Backup\BitBucket\negrini.musiartist\src\MusiArtist.MVC\Areas\admin\Controllers\BaseController.cs:29 System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +180 System.Web.Mvc.<>c__DisplayClass19.<BeginExecute>b__13() +18 System.Web.Mvc.Async.AsyncResultWrapper.<.cctor>b__0(IAsyncResult asyncResult, Action action) +15
System.Web.Mvc.Async.WrappedAsyncResult
System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +49
1.CallEndDelegate(IAsyncResult的 asyncResult)+29
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
System.Web.Mvc.Async.WrappedAsyncVoid
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End()+49
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult) 结果)+9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9765121 System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean&amp; completedSynchronously)+155
答案 0 :(得分:2)
我对&#34; DisableAsyncSupport&#34;进行了覆盖。控制器的方法,并将getter设置为false,一切正常!
protected override bool DisableAsyncSupport
{
get { return false; }
}
我相信通过继承自控制器(我有自己的控制器,&#34; ControllerBase&#34;),该方法的行为必须不同。