An asynchronous module or handler completed while an asynchronous operation was still pending

RobVious picture RobVious · Oct 4, 2013 · Viewed 7.2k times · Source

I had the following for emailing, which worked:

    private SmtpClient _client = new SmtpClient("smtp.gmail.com", 587)
    {
        Credentials = new NetworkCredential("[email protected]", "password"),
        EnableSsl = true
    };
    public void DoThis(){
    _client.Send("[email protected]", to.Email, "Subject", "Body");}
    public void DoThat(){
    _client.Send("[email protected]", to.Email, "Subject", "Body");}

But it was blocking the web application until the email was sent, so I decided to try sending Asynchronously:

   public void DoThis(){
    var message = new MailMessage("[email protected]", to.Email, "Subject", "Body");
   _client.SendAsync(message, null);
   }

Which I can see getting treated asynchronously if I debug, but I always get the following:

An asynchronous module or handler completed while an asynchronous operation was still pending.

What am I doing wrong?

Answer

RobVious picture RobVious · Nov 7, 2013

I ended up reworking the way my Async email is sent with the following:

public void SendAsyncEmail(MailMessage message)
        {

            var client = new SmtpClient("mail.hover.com", 587)
            {
                Credentials = new NetworkCredential("[email protected]", "Covert00!"),
                EnableSsl = false
            };
            client.SendCompleted += (sender, error) =>
            {
                if (error.Error != null)
                {
                    // TODO: get this working
                    throw new WarningException("Email Failed to send.");
                }
                client.Dispose();
                message.Dispose();
            };
            ThreadPool.QueueUserWorkItem(o => client.SendAsync(message, Tuple.Create(client, message)));
        }

Note that this is a temporary solution, and that the correct way (it seems) to handle emailing is to use some kind of queue service paired with a worker role or other windows service to pop the messages.