0
私のプロジェクトでMVC IDを使用しています。私は、ユーザー登録後、次のエラーを受信し続ける:「非同期 操作がまだ保留している間に完了した非同期モジュールまたはハンドラ」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
非常に奇妙です。ほとんどの場合、非同期コードが非同期voidメソッドを呼び出している場合は、この例外が発生する唯一の時間です。しかし、私はあなたがそれをやっている投稿されたコードの場所を見ません。 'async void'を使用することは、アンチパターンです。そのため、あなたが行っているかもしれない場所でプロジェクトをチェックし、代わりに' async Task'を使用してください。 –