线程化 Windows 服务 - SmtpClient 问题
我们正在使用 topshelf/windsor/masstransit 编写一个线程服务,从 MSMQ 中获取消息,然后发送带有 PDF 附件的电子邮件。
SmtpClient.Send() 方法时不时地抛出此错误消息:
异步调用已在进行中。必须先完成或取消它,然后才能调用此方法。
我在 Windsor 中将 SmtpClient 设置为 .LifeStyle.Transient,所以我的理解是每个线程都应该获得自己的实例?
知道如何解决这个问题,除了锁定那部分代码之外。这会破坏线程的目的。
提前致谢
We're writing a threaded service using topshelf / windsor / masstransit to pick up messages off of MSMQ and then send an e-mail off with PDF attachment.
Every now and again the SmtpClient.Send() method throws this error message:
An asynchronous call is already in progress. It must be completed or canceled before you can call this method.
I've got the SmtpClient set up as .LifeStyle.Transient in Windsor, so my understanding is that each thread should get its own instance?
Any idea how I can around this, short of locking that part of the code. Which would kind of defeat the purpose of the threads.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Transient
不应该只为每个线程创建一个新实例,而应该为每个请求创建一个新实例。我会尝试确认您确实获得了 SmtpClient 的新实例 - 确保您没有将其粘贴在静态字段中,或者以某种方式以相同的引用结束。您可以尝试在每次发送电子邮件时将
GetHashCode()
的结果吐入控制台,以查看它们是否都使用相同的对象。如果做不到这一点,我唯一的建议是尝试将其简化为一个非常简单的示例 - 如果您在执行此操作时没有发现问题,您可以在此处发布代码,有人可能会提供帮助。
Transient
shouldn't just create a new instance per thread, but a new instance for every request.I'd try to confirm that you're truly getting a new instance of SmtpClient - make sure you're not sticking it in a static field or otherwise somehow ending up with the same reference. You could try spitting the result from
GetHashCode()
into the Console each time you send an email to see if they're all using the same object.Failing that, my only suggestion is to try and strip it down to a really simple example - if you don't find the issue in doing this, you can post the code here and someone might be able to help.