Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVCMailer SendAsync()与Amazon SES一起失败_C#_Asp.net Mvc_Amazon Ses_Mvcmailer - Fatal编程技术网

C# MVCMailer SendAsync()与Amazon SES一起失败

C# MVCMailer SendAsync()与Amazon SES一起失败,c#,asp.net-mvc,amazon-ses,mvcmailer,C#,Asp.net Mvc,Amazon Ses,Mvcmailer,如果我将Send()与MVCMailer一起使用,我的SES可以正常工作,但是SendAsync()会显示下面的错误消息,有人知道解决方法吗?谢谢 System.Net.Mail.SmtpException: Failure sending mail. ---> System.InvalidOperationException: An asynchronous operation cannot be started at this time. Asynchronous operations

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

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)
System.Net.Mail.SmtpException:发送邮件失败。-->System.InvalidOperationException:此时无法启动异步操作。异步操作只能在异步处理程序或模块内启动,或在页面生命周期中的某些事件期间启动。如果在执行页面时发生此异常,请确保已标记该页面。
在System.Web.AspNetSynchronizationContext.OperationStarted()中
位于System.ComponentModel.AsyncOperation.CreateOperation(对象userSuppliedState,SynchronizationContext syncContext)
位于System.Net.Mail.SmtpClient.SendAsync(邮件消息,对象用户令牌)
---内部异常堆栈跟踪的结束---
位于System.Net.Mail.SmtpClient.SendAsync(邮件消息,对象用户令牌)
在Mvc.Mailer.SmtpClientWrapper.SendAsync(MailMessage-MailMessage,Object-userState)
在Mvc.Mailer.MailMessageExtensions.SendAsync(MailMessage消息、对象用户状态、ISmtpClient smtpClient)
在c:\Users\William Business\Desktop\TWB\TWB Central\Projects\awesomesaude\awesomesaude\Helpers\AccountHelper\Helpers\AccountHelper.cs第316行中的MVCWebsite.Helpers.AccountHelper.RegisterNewUser(BasedContext db,AccountViewModelForReg VM,Boolean isCaptchaValid,Boolean modelValidity)中
在MVCWebsite.Controllers.AccountController.Register(AccountViewModelForReg VM,布尔captchaValid)中的c:\Users\William Business\Desktop\TWB\TWB Central\Projects\AWESOMESOUP\AWESOMESOUP\Controllers\AccountController.cs:第308行
在lambda_方法中(闭包、控制器基、对象[])
位于System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller,Object[]参数)
位于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.b__41()中
在System.Web.Mvc.Async.AsyncResultRapper.c_uuu显示类8`1.b_uuu7(IAsyncResult)
位于System.Web.Mvc.Async.AsyncResultRapper.WrappedAsyncResult`1.End()
位于System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
在System.Web.Mvc.Async.AsyncControllerActionInvoker.c_uuDisplayClass37.c_uuDisplayClass39.b_u33()中
在System.Web.Mvc.Async.AsyncControllerActionInvoker.c__DisplayClass4f.b__49()中
在System.Web.Mvc.Async.AsyncControllerActionInvoker.c__DisplayClass4f.b__49()中
在System.Web.Mvc.Async.AsyncControllerActionInvoker.c__DisplayClass37.b__36(IAsyncResult asyncResult)
位于System.Web.Mvc.Async.AsyncResultRapper.WrappedAsyncResult`1.End()
位于System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)
在System.Web.Mvc.Async.AsyncControllerActionInvoker.c_uuDisplayClass25.c_uuDisplayClass2A.b_u20()中
在System.Web.Mvc.Async.AsyncControllerActionInvoker.c__DisplayClass25.b__22(IAsyncResult asyncResult)

此错误是故意造成的。您可以全局关闭此选项:

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

编辑

启用AllowAsyncDuringSyncStages或使用任务并行库都有潜在的缺点。使用AsyncController没有这两个缺点。感谢@StephenCleary质疑我的答案

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();
  }
}

你有没有下过决心?我也遇到了同样的问题。事实证明,这不是MVC编译器的问题,而是MVC的一些线程问题。看看我在这个帖子中的答案:@Baconbeastnz你能评论一下为什么你批准了我的答案吗?相关问题:这个设置危险吗?是的,环境很危险。我会推荐
Send
over
StartNew
wrapping
SendAsync
@StephenCleary你推荐StartNew(()=>…Send())还是StartNew(()=>…SendAsync())?@JJS:不,我推荐
Send
over
StartNew(()=>…SendAsync())
@StephenCleary为什么建议使用StartNew(…)通过任一形式的异步发送straignt sync调用?
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();
  }
}