MVCMailer SendAsync()失败与亚马逊SES [英] MVCMailer SendAsync() fails with Amazon SES

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

问题描述

如果我使用的send()与MVCMailer,我SES工作正常,但SendAsync()显示下面的错误消息,没有人知道一个工作围绕这个的?谢谢!

  System.Net.Mail.SmtpException:发送邮件失败。 ---> System.InvalidOperationException:异步操作不能在这个时候开始。异步操作只能在异步处理程序或模块内或在在页面生命周期的某些事件开始。如果在执行一个页面出现了这种异常,保证页面被标记为LT;%@页面异步=真正的%取代。
   在System.Web.AspNetSynchronizationContext.OperationStarted()
   在System.ComponentModel.AsyncOperation.CreateOperation(对象userSuppliedState,SynchronizationContext的syncContext)
   在System.Net.Mail.SmtpClient.SendAsync(消息MAILMESSAGE,对象userToken)
   ---内部异常堆栈跟踪的结尾---
   在System.Net.Mail.SmtpClient.SendAsync(消息MAILMESSAGE,对象userToken)
   在Mvc.Mailer.SmtpClientWrapper.SendAsync(MAILMESSAGE MAILMESSAGE,对象userState)
   在Mvc.Mailer.MailMessageExtensions.SendAsync(消息MAILMESSAGE,对象userState,ISmtpClient smtpClient)
   在MVCWebsite.Helpers.AccountHelper.RegisterNewUser(BaseDBContext分贝,AccountViewModelForReg VM,布尔isCaptchaValid,布尔modelValidity)在C:\\用户\\威廉业务:\\桌面\\ TWB \\ TWB中央\\项目\\ AwesomeSauce \\ AwesomeSauce \\助手\\ AccountHelper.cs: 316行
   在MVCWebsite.Controllers.AccountController.Register(AccountViewModelForReg VM,布尔captchaValid)在C:\\用户\\威廉业务:\\桌面\\ TWB \\ TWB中央\\项目\\ AwesomeSauce \\ AwesomeSauce \\ \\控制器AccountController.cs:308线
   在lambda_method(封闭,ControllerBase,对象[])
   在System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase控制器,对象[]参数)
   在System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary`2参数)
   在System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext,ActionDescriptor actionDescriptor,IDictionary`2参数)
   在System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41()
   在System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
   在System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
   在System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult的asyncResult)
   在System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33()
   在System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()
   在System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()
   在System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult)
   在System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
   在System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult的asyncResult)
   在System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<>c__DisplayClass2a.<BeginInvokeAction>b__20()
   在System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult)


解决方案

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

 &LT;&的appSettings GT;
  &LT;添加键=ASPNET:AllowAsyncDuringSyncStagesVALUE =FALSE/&GT;
&LT; /的appSettings&GT;

http://msdn.microsoft.com/en-us/library /hh975440.aspx

有些东西是用做任务异步的背景下,从ASP.Net使用调度解耦的是要考虑。使用这种方法,你就不需要使用改变appSetting。

 使用Mvc.Mailer;
...
公众的ActionResult SendWelcomeMessage()
{
    Task.Factory.StartNew(()=方式&gt; UserMailer.Welcome()SendAsync());
    返回RedirectToAction(「指数」);
}


修改

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

 公共类HomeController的:AsyncController
{
  公共无效SendMessageAsync()
  {
    VAR的客户=新SmtpClientWrapper();
    client.SendCompleted + =(发件人,参数)=&GT;
      AsyncManager.OutstandingOperations.Decrement();
    AsyncManager.OutstandingOperations.Increment();
    新UserMailer()欢迎()SendAsync(,客户端)。;
  }  公众的ActionResult SendMessageCompleted()
  {
    返回查看();
  }
}

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:\Users\William-Business\Desktop\TWB\TWB Central\Projects\AwesomeSauce\AwesomeSauce\Helpers\AccountHelper.cs:line 316
   at MVCWebsite.Controllers.AccountController.Register(AccountViewModelForReg VM, Boolean captchaValid) in c:\Users\William-Business\Desktop\TWB\TWB Central\Projects\AwesomeSauce\AwesomeSauce\Controllers\AccountController.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>

reference http://msdn.microsoft.com/en-us/library/hh975440.aspx

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 use change the appSetting.

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


Edit

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()失败与亚马逊SES的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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