How to debug this error? I can step through the whole request without getting any error. EventViewer does not reveal more details. At the end it shows this error page:
Server Error in '/' Application.
An asynchronous module or handler completed while an asynchronous operation was still pending.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: An asynchronous module or handler completed while an asynchronous operation was still pending.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[InvalidOperationException: An asynchronous module or handler completed while an asynchronous operation was still pending.]
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.1055.0
Controller called:
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
user.Claims.Add(new Microsoft.AspNet.Identity.EntityFramework.IdentityUserClaim() { ClaimType = "SOrientation", ClaimValue = "Derpe", UserId = user.Id });
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
/// enables two factor auth for this user
/// registration of twofact auth providers in identityConfig.cs/UserManager.Create
await UserManager.SetTwoFactorEnabledAsync(user.Id, true);
var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>");
ViewBag.Link = callbackUrl;
return View("DisplayEmail");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
The controller action runs through without any problems!
I narrowed down the problem to this async method call:
await UserManager.SendEmailAsync
which calls this method:
public Task SendAsync(IdentityMessage m)
{
var c = new SmtpClient("smtp.gmail.com", 587);
//c.SendCompleted += MailSendCompleted;
c.EnableSsl = true;
c.DeliveryMethod = SmtpDeliveryMethod.Network;
//c.Host = "smtp.gmail.com";
c.Credentials = new System.Net.NetworkCredential("jxxx@gmail.com", "xxx");
c.SendAsync("xxx@gmail.de", m.Destination, m.Subject, m.Body, m.Destination);
return Task.FromResult(0);
}
What's wrong with the method?
While the follwoing code works, will it run async?
public Task SendAsync(IdentityMessage m)
{
var c = new SmtpClient("smtp.gmail.com", 587);
//c.SendCompleted += MailSendCompleted;
c.EnableSsl = true;
c.DeliveryMethod = SmtpDeliveryMethod.Network;
//c.Host = "smtp.gmail.com";
c.Credentials = new System.Net.NetworkCredential("jxxx@gmail.com", "xxx");
var t = Task.Run(() => c.Send("xx@gmx.de", m.Destination, m.Subject, m.Body));
await t;
}