SmtpClient.SendAsync 调用自动取消

发布于 2024-12-18 18:35:56 字数 2016 浏览 2 评论 0原文

每当我从 ASP.NET MVC 应用程序中调用 smtpClient.SendAsync(...) 时,异步请求都会自动取消,即使 SendAsyncCancel() 从未被调用。

另一方面,同步请求的执行情况良好。

我的 EmailService 服务包装器处理在 ASP.NET MVC 3 应用程序中使用 SmtpClient 发送异步电子邮件。服务实例通过 StructureMap 注入到每个 MVC 控制器中,它将新的 SmtpClient 实例包装在 using (...) { } 语句中。

这是我的 SmtpClientEmailService.SendAsync 包装方法:

public void SendAsync(EmailMessage message)
{
    try
    {
        using (var smtpClient = new SmtpClient(_cfg.Host, _cfg.Port)
        {
            EnableSsl = _cfg.EnableSsl,
            Credentials = _credentials
        })
        {
            smtpClient.SendCompleted += new SendCompletedEventHandler(Email_OnCompleted);

            var mailMessage = new MailMessage(message.From, message.To)
                                {
                                    Subject = message.Subject,
                                    Body = message.Body
                                };

            smtpClient.SendAsync(mailMessage, message);

            _logger.Info(string.Format("Sending async email to {0} with subject [{1}]", message.To, message.Subject));
        }
    }
    catch (Exception ex)
    {
        _logger.Error("Async email error: " + ex);
        throw;
    }

}

这是我的 Email_OnCompleted 委托:

public void Email_OnCompleted(object sender, AsyncCompletedEventArgs e)
{
    var mail = (EmailMessage)e.UserState;

    if (e.Error != null)
    {
        _logger.Error(string.Format("Error sending email to {0} with subject [{1}]: {2}", mail.To, mail.Subject, e.Error));
    }
    else if (e.Cancelled)
    {
        _logger.Warn(string.Format("Cancelled email to {0} with subject [{1}].", mail.To, mail.Subject));
    }
    else
    {
        _logger.Info(string.Format("Sent email to {0} with subject [{1}].", mail.To, mail.Subject));
    }
}

为什么异步电子邮件被取消,但同步电子邮件却通过还好吗?会不会是处置问题?

Whenever I call smtpClient.SendAsync(...) from within my ASP.NET MVC application, the asynchronous requests are automatically cancelled, even though SendAsyncCancel() is never called.

Synchronous .Send(...) requests, on the other hand, go through just fine.

My EmailService service wrapper handles sending asynchronous email with SmtpClient from within my ASP.NET MVC 3 application. A service instance is injected into each MVC controller by StructureMap, which wraps a new SmtpClient instance in a using (...) { } statement.

Here is my EmailService.SendAsync wrapper method for SmtpClient:

public void SendAsync(EmailMessage message)
{
    try
    {
        using (var smtpClient = new SmtpClient(_cfg.Host, _cfg.Port)
        {
            EnableSsl = _cfg.EnableSsl,
            Credentials = _credentials
        })
        {
            smtpClient.SendCompleted += new SendCompletedEventHandler(Email_OnCompleted);

            var mailMessage = new MailMessage(message.From, message.To)
                                {
                                    Subject = message.Subject,
                                    Body = message.Body
                                };

            smtpClient.SendAsync(mailMessage, message);

            _logger.Info(string.Format("Sending async email to {0} with subject [{1}]", message.To, message.Subject));
        }
    }
    catch (Exception ex)
    {
        _logger.Error("Async email error: " + ex);
        throw;
    }

}

Here is my Email_OnCompleted delegate:

public void Email_OnCompleted(object sender, AsyncCompletedEventArgs e)
{
    var mail = (EmailMessage)e.UserState;

    if (e.Error != null)
    {
        _logger.Error(string.Format("Error sending email to {0} with subject [{1}]: {2}", mail.To, mail.Subject, e.Error));
    }
    else if (e.Cancelled)
    {
        _logger.Warn(string.Format("Cancelled email to {0} with subject [{1}].", mail.To, mail.Subject));
    }
    else
    {
        _logger.Info(string.Format("Sent email to {0} with subject [{1}].", mail.To, mail.Subject));
    }
}

Why are async emails being cancelled, but synchronous emails go through just fine? Could it be a dispose issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如梦 2024-12-25 18:35:56

这绝对可能是一个处置问题。当您处置客户端时,它会取消任何未完成的异步操作。

您应该在 Email_OnCompleted 中处置客户端。

关于在何处处置的 SO 帖子:在 SendComplete 中处置 SmtpClient?

It can definitely be a dispose issue. When you dispose the client it cancels any outstanding async operations.

You should dispose the client in Email_OnCompleted.

An SO post on where to dispose: Dispose SmtpClient in SendComplete?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文