This project has moved. For the latest updates, please go here.

Email Listener is Asych which fails in ASP.NET


Symptom: Intermittent failure of email listener to send email, even when flooding control is turned off.

Cause: The email listener is asynch, which just dies when invoked from the context of an ASP.NET page.

2014-08-07 19:51:51Z [13] Verbose System.Net 0: [18424] SmtpClient#19832188::SendAsync(DeliveryMethod=Network)
2014-08-07 19:51:51Z [13] Error System.Net 0: [18424] Exception in SmtpClient#19832188::Send - Asynchronous operations are not allowed in this context. Page starting an asynchronous operation has to have the Async attribute set to true and an asynchronous operation can only be started on a page prior to PreRenderComplete event..
at System.Web.LegacyAspNetSynchronizationContext.OperationStarted()
at System.ComponentModel.AsyncOperation.CreateOperation(Object userSuppliedState, SynchronizationContext syncContext)
at System.Net.Mail.SmtpClient.SendAsync(MailMessage message, Object userToken)
2014-08-07 19:51:51Z [13] Verbose System.Net 0: [18424] Exiting SmtpClient#19832188::SendAsync()

Suggested fix: Check for HttpContext.Current -- if it isn't Null, don't send Asych or at least allow this asych behavior to be turned off.


jacobsmidt wrote Mar 26 at 10:32 PM

I think I am experiencing the same thing. I get this exception when using the Email trace listener in ASP.NET environment:

System.InvalidOperationException: "An asynchronous module or handler completed while an asynchronous operation was still pending"

I suggest using a buffer queue with background thread instead of async framework.