MVCMailer SendAsync() 与 Amazon SES 一起失败 [英] MVCMailer SendAsync() fails with Amazon SES

查看:27
本文介绍了MVCMailer SendAsync() 与 Amazon SES 一起失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将 Send() 与 MVCMailer 一起使用,我的 SES 工作正常,但 SendAsync() 显示以下错误消息,有人知道解决此问题的方法吗?谢谢!

If I use Send() with MVCMailer, my SES works fine, but SendAsync() shows the error message below, does anyone know of a work around for this? THanks!

System.Net.Mail.SmtpException: Failure sending mail. ---> System.InvalidOperationException: An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>.
   at System.Web.AspNetSynchronizationContext.OperationStarted()
   at System.ComponentModel.AsyncOperation.CreateOperation(Object userSuppliedState, SynchronizationContext syncContext)
   at System.Net.Mail.SmtpClient.SendAsync(MailMessage message, Object userToken)
   --- End of inner exception stack trace ---
   at System.Net.Mail.SmtpClient.SendAsync(MailMessage message, Object userToken)
   at Mvc.Mailer.SmtpClientWrapper.SendAsync(MailMessage mailMessage, Object userState)
   at Mvc.Mailer.MailMessageExtensions.SendAsync(MailMessage message, Object userState, ISmtpClient smtpClient)
   at MVCWebsite.Helpers.AccountHelper.RegisterNewUser(BaseDBContext db, AccountViewModelForReg VM, Boolean isCaptchaValid, Boolean modelValidity) in c:UsersWilliam-BusinessDesktopTWBTWB CentralProjectsAwesomeSauceAwesomeSauceHelpersAccountHelper.cs:line 316
   at MVCWebsite.Controllers.AccountController.Register(AccountViewModelForReg VM, Boolean captchaValid) in c:UsersWilliam-BusinessDesktopTWBTWB CentralProjectsAwesomeSauceAwesomeSauceControllersAccountController.cs:line 308
   at lambda_method(Closure , ControllerBase , Object[] )
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41()
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<>c__DisplayClass2a.<BeginInvokeAction>b__20()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult)

推荐答案

此错误是设计使然.您可以全局关闭此功能:

This error is by design. You can turn this off globally:

<appSettings>
  <add key="aspnet:AllowAsyncDuringSyncStages" value="false" />
</appSettings>

参考 http://msdn.microsoft.com/en-us/图书馆/hh975440.aspx

还需要考虑使用 Task 在后台执行异步操作,将其与 ASP.Net 使用的调度程序分离.使用此方法,您无需更改 appSetting.

Something else to consider is using a Task to do the async in the background, decoupling it from the scheduler that ASP.Net uses. Using this method, you wouldn't need to change the appSetting.

using Mvc.Mailer;
...
public ActionResult SendWelcomeMessage()
{
    Task.Factory.StartNew(() => UserMailer.Welcome().SendAsync());
    return RedirectToAction("Index");
}

<小时>

编辑

启用 AllowAsyncDuringSyncStages 或使用任务并行库都有潜在的缺点.使用 AsyncController 没有任何缺点.感谢@StephenCleary 挑战我的答案.

Enabling AllowAsyncDuringSyncStages or using the Task Parallel Library both have potential drawbacks. Using AsyncController does not have either drawback. Thanks @StephenCleary for challenging my answer.

public class HomeController : AsyncController
{
  public void SendMessageAsync()
  {
    var client = new SmtpClientWrapper();
    client.SendCompleted += (sender, args) => 
      AsyncManager.OutstandingOperations.Decrement();
    AsyncManager.OutstandingOperations.Increment();
    new UserMailer().Welcome().SendAsync("", client);
  }

  public ActionResult SendMessageCompleted()
  {
    return View();
  }
}

这篇关于MVCMailer SendAsync() 与 Amazon SES 一起失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆