Asynchronous Action Error (Async)

Asked

Viewed 89 times

0

This action should send an Asynchronous email, all very simple so far, including using the same class in other projects with the same version of MVC. But to my surprise, this project does not run. Simply appears the error below.

Error:

The asynchronous action method 'Forgettingpassword' Returns a Task, which cannot be executed synchronously.

Stacktrace:

at System.Web.Mvc.Async.Taskasyncactiondescriptor.Execute(Controllercontext controllerContext, Idictionary2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 Parameters) at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.b__12() at System.Web.Mvc.ControllerActionInvoker.Invokeactionmethodfilter(Iactionfilter filter, Actionexecutingcontext preContext, Func`1 continuation) at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15. <>c__DisplayClass17.b__14() at System.Web.Mvc.ControllerActionInvoker.Invokeactionmethodwithfilters(Controllercontext

Action:

 [AllowAnonymous]
    [HttpPost, ValidateAntiForgeryToken]
    public async Task<ActionResult> EsqueciMinhaSenha(UsuarioEsqueciMinhaSenhaViewModel vModel)
    {
        if (ModelState.IsValid)
        {
            var conteudo = "este é o conteudo do email";
            var nomeDestinatario = "esse é o nome do destinatário";

            if(await EmailService.SendAsync(Language.PasswordRecovery, conteudo, vModel.EmailOuUsername, nomeDestinatario))
            {
                TempData["MensagemRetorno"] = Language.EmailSendedWithSuccess;
                return View("login");
            }
        }

        TempData["MensagemRetorno"] = Language.ErrorSendingEmail;
        return View("EsqueciMinhaSenha");
    }

Sending the e-mail:

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);
}
  • What version of . NET Framework?

  • @jbueno Solution projects are all set to 4.5

  • And you have the 4.6 available? Try to change the project that is wrong to 4.6 and test, I saw somewhere that there was a bug that caused exactly this in the 4.5 beta. So this could be it.

  • @jbueno I will try, but I believe it will not change anything, if I am not mistaken I have already done the reverse, I think it was 4.6 and I switched to 4.5... but I actually mentioned there that I have another project in the same versions and everything works perfectly... the same code and etc.

  • the error is local or on the server?

  • @sir_ask is local, I haven’t uploaded any version yet

  • http://stackoverflow.com/questions/14994335/the-asynchronous-action-method-returns-a-task-which-cannot-be-executed-synchron

  • confirms that all dll’s q have are ok,

  • @sir_ask this all ok! Even before posting here I’ve looked at thousands of posts about and nothing solves.

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.