关于 c#:Threaded Windows 服务 – SmtpClient 问题 | 珊瑚贝

Threaded Windows Service – SmtpClient Issues


我们正在编写一个使用 topshelf / windsor / masstransit 的线程服务来从 MSMQ 中提取消息,然后发送一封带有 PDF 附件的电子邮件。

SmtpClient.Send() 方法时不时会抛出此错误消息:

An asynchronous call is already in progress. It must be completed or canceled before you can call this method.

我在 Windsor 中将 SmtpClient 设置为 .LifeStyle.Transient,所以我的理解是每个线程都应该有自己的实例?

知道我可以如何解决这个问题,而不是锁定那部分代码。这会破坏线程的目的。

提前致谢


Transient 不应该只是为每个线程创建一个新实例,而是为每个请求创建一个新实例。

我会尝试确认您确实获得了一个新的 SmtpClient 实例 – 确保您没有将其粘贴在静态字段中或以其他方式以相同的引用结束。您可以尝试在每次发送电子邮件时将结果从 GetHashCode() 吐到控制台中,看看他们是否都在使用同一个对象。

如果做不到这一点,我唯一的建议是尝试将其简化为一个非常简单的示例 – 如果您没有发现这样做的问题,您可以在此处发布代码,有人可能会提供帮助。

  • 是的,瞬态总是给你一个新的实例,没有例外。任何导致问题的堆栈跟踪和上下文都会很好。我怀疑这与温莎有直接关系。
  • GetHashCode 的想法很有帮助。最终成为只被实例化一次的拥有类(消费者)。移动了一些代码,现在可以正常工作了!谢谢您的帮助!


来源:https://www.codenong.com/5144023/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9437.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?