在异步操作仍处于挂起状态时完成异步模块或处理程序

时间:2021-01-16 21:02:26

I had the following for emailing, which worked:

我有以下电子邮件,其中有效:

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

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

但是在发送电子邮件之前它阻止了Web应用程序,所以我决定尝试发送异步:

   public void DoThis(){
    var message = new MailMessage("from@gmail.com", 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?

我究竟做错了什么?

1 个解决方案

#1


5  

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

我最后用以下方式重新编写了我的Async电子邮件的方式:

public void SendAsyncEmail(MailMessage message)
        {

            var client = new SmtpClient("mail.hover.com", 587)
            {
                Credentials = new NetworkCredential("admin@site.com", "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.

请注意,这是一个临时解决方案,处理电子邮件的正确方法(似乎)是使用某种与工作角色或其他Windows服务配对的队列服务来弹出消息。

#1


5  

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

我最后用以下方式重新编写了我的Async电子邮件的方式:

public void SendAsyncEmail(MailMessage message)
        {

            var client = new SmtpClient("mail.hover.com", 587)
            {
                Credentials = new NetworkCredential("admin@site.com", "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.

请注意,这是一个临时解决方案,处理电子邮件的正确方法(似乎)是使用某种与工作角色或其他Windows服务配对的队列服务来弹出消息。