Asp.net mvc 在MVC标识中发送邮件时发生异步错误
我正在我的一个项目中使用MVC标识。注册用户后,我一直收到以下错误: “异步模块或处理程序在异步 行动仍在进行中。” 我已经在控制器中实现了Asp.net mvc 在MVC标识中发送邮件时发生异步错误,asp.net-mvc,vb.net,Asp.net Mvc,Vb.net,我正在我的一个项目中使用MVC标识。注册用户后,我一直收到以下错误: “异步模块或处理程序在异步 行动仍在进行中。” 我已经在控制器中实现了wait,看不出哪里出了问题,如果有人可以建议的话。非常感谢 控制器: <HttpPost> <AllowAnonymous> <ValidateAntiForgeryToken> Public Async Function Register(model As RegisterViewMod
wait
,看不出哪里出了问题,如果有人可以建议的话。非常感谢
控制器:
<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
您需要使服务功能异步并等待。非常奇怪。在大多数情况下,只有当异步代码调用异步void方法时,才会出现此异常。但是,我在发布的代码中没有看到任何地方可以这样做。无论如何,使用
async void
是一种反模式,因此请检查您的项目中可能正在执行此操作的位置,并改用async Task
。
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