I have this string to format and the exception is thrown on this part (string body ....)
private Task SendEmailConfirmation(UserModel user)
{
var emailService = new EmailUnsecServiceAgent(null);
string body = string.Format("Dear {0} <BR/>Thank you for your registration, " +
"please click on the below link to complete your" +
" registration: <a href=\"{1}\" title=\"User Email Confirm\">{1}</a>",
user.UserName,
Url.Action("ConfirmEmail",
"Account",
new
{
confirmationToken = user.ConfirmationToken,
email = user.Email
},,
Request.Url.Scheme));
return Task.Run(() => emailService.SendEmail("Confirm your account", body, null, true, null, null, null));
}
confirmationToken
and email
are strings and my ConfirmEmail is
[AllowAnonymous]
public async Task<ActionResult> ConfirmEmail(string confirmationToken, string email)
{
var securityService = new SecurityUnsecServiceAgent(null);
UserModel user = securityService.GetUserByConfirmationToken(confirmationToken);
if (user != null)
{
if (user.Email == email)
{
user.ConfirmedEmail = true;
securityService.UpdateUser(user);
return View("RegisterMessage");
}
return View("ConfirmEmail", user);
}
return RedirectToAction("ConfirmEmail", user);
}
and this is the StackTrace:
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode)
at System.Web.Hosting.IIS7WorkerRequest.GetServerVariableInternal(String name)
at System.Web.Hosting.IIS7WorkerRequest.GetServerVariable(String name)
at System.Web.WebPages.UrlRewriterHelper.WasThisRequestRewritten(HttpContextBase httpContext)
at System.Web.WebPages.UrlRewriterHelper.WasRequestRewritten(HttpContextBase httpContext)
at System.Web.WebPages.UrlUtil.GenerateClientUrlInternal(HttpContextBase httpContext, String contentPath)
at System.Web.WebPages.UrlUtil.GenerateClientUrl(HttpContextBase httpContext, String contentPath)
at System.Web.Mvc.UrlHelper.GenerateUrl(String routeName, String actionName, String controllerName, RouteValueDictionary routeValues, RouteCollection routeCollection, RequestContext requestContext, Boolean includeImplicitMvcValues)
at System.Web.Mvc.UrlHelper.GenerateUrl(String routeName, String actionName, String controllerName, RouteValueDictionary routeValues)
at System.Web.Mvc.UrlHelper.Action(String actionName, String controllerName, Object routeValues)
at SendEmailConfirmation(UserModel user) in c:\Projects\..\Controllers\AccountController.cs:line 361
at Controllers.AccountController.<>c__DisplayClass21.<Register>b__1d() in c:\Projects\..\Controllers\AccountController.cs:line 318
at System.Threading.Tasks.Task`1.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
The URL helper might be trying to create a URL after the request has already been destroyed (because it's async). In that case you will get an error when trying Url.Action().
Try generating the body of the email in a non-async controller and send it into your email class as an argument.
The question Custom task runner method throws ArgumentException seems to be closely related to what you're seeing (down to the stacktrace and the method the OP arrived at an answer).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With