【问题标题】:Async error while sending mail in MVC identitiy以 MVC 身份发送邮件时出现异步错误
【发布时间】:2017-05-12 14:04:45
【问题描述】:

我在我的一个项目中使用 MVC 身份。注册用户后一直收到以下错误:

"异步模块或处理程序在异步完成时完成 操作仍在等待中。”

我已经在我的控制器中实现了Await,但看不到我哪里出错了,如果有人可以请指教。非常感谢。

控制器:

    <HttpPost>
    <AllowAnonymous>
    <ValidateAntiForgeryToken>
    Public Async Function Register(model As RegisterViewModel) As Task(Of ActionResult)
        If ModelState.IsValid Then
            Dim user = New ApplicationUser() With {
            .UserName = model.Email,
            .Email = model.Email,
            .FullName = model.FullName,
            .UserSettings = New UserSettings() With {
            .OrderEnabled1 = True, .OrderEnabled2 = True, .OrderEnabled3 = True, .OrderEnabled4 = True, .OrderEnabled5 = True, .OrderEnabled6 = True, .OrderEnabled7 = True,
            .OrderInput1 = 1, .OrderInput2 = 2, .OrderInput3 = 3, .OrderInput4 = 4, .OrderInput5 = 5, .OrderInput6 = 6, .OrderInput7 = 7,
            .VAT = VATconst
            }
        }

            user.UserSettings.Id = user.Id

            Dim result = Await UserManager.CreateAsync(user)

            If result.Succeeded Then
                UserManager.AddToRole(user.Id, "User")

                Dim code = Await UserManager.GenerateEmailConfirmationTokenAsync(user.Id)
                Dim callbackUrl = Url.Action("ConfirmEmail", "Account", New With {.userId = user.Id, .code = code}, protocol:=Request.Url.Scheme)
                Await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=""" & callbackUrl & """>here</a>")

                Return RedirectToAction("Index", "Users")

            End If
            AddErrors(result)
        End If

        Return View(model)
    End Function

电子邮件服务:

Public Class EmailService
Implements IIdentityMessageService

Public Function SendAsync(message As IdentityMessage) As Task Implements IIdentityMessageService.SendAsync
    ' Plug in your email service here to send an email.

    ' Create the mail message
    Dim msg As New MailMessage()
    msg.To.Add(message.Destination)

    Dim address As New MailAddress("*****@gmail.com")
    msg.From = address
    msg.Subject = message.Subject
    msg.Body = message.Body
    msg.IsBodyHtml = True

    'Configure an SmtpClient to send the mail.            
    Dim client As New SmtpClient()
    client.DeliveryMethod = SmtpDeliveryMethod.Network
    client.EnableSsl = True
    client.Host = "smtp.gmail.com"
    client.Port = 25

    Dim credentials As New NetworkCredential("*****@gmail.com", "****")
    client.Credentials = credentials

    'Send the msg
    Return client.SendMailAsync(msg)

End Function
End Class

【问题讨论】:

  • 很奇怪。在大多数情况下,只有在异步代码调用 async void 方法时才会出现此异常。但是,我在发布的代码中看不到您正在执行此操作的任何地方。无论如何,使用async void 是一种反模式,因此请检查您的项目是否有可能这样做的地方并改用async Task

标签: asp.net-mvc vb.net


【解决方案1】:

您还需要使您的服务功能异步并等待。

【讨论】:

    猜你喜欢
    • 2015-11-01
    • 2016-07-27
    • 1970-01-01
    • 2019-10-20
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-24
    相关资源
    最近更新 更多