SmtpClient.SendAsync 调用自动取消
每当我从 ASP.NET MVC 应用程序中调用 smtpClient.SendAsync(...)
时,异步请求都会自动取消,即使 SendAsyncCancel()
从未被调用。
另一方面,同步请求的执行情况良好。
我的 EmailService
服务包装器处理在 ASP.NET MVC 3 应用程序中使用 SmtpClient
发送异步电子邮件。服务实例通过 StructureMap 注入到每个 MVC 控制器中,它将新的 SmtpClient
实例包装在 using (...) { }
语句中。
这是我的 SmtpClient
的 EmailService.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这绝对可能是一个处置问题。当您处置客户端时,它会取消任何未完成的异步操作。
您应该在
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?