在 .NET 4.0 下使用 SmtpClient、SendAsync 和 Dispose 的最佳实践是什么
我对如何管理 SmtpClient 感到有点困惑,因为它是一次性的,特别是如果我使用 SendAsync 进行调用。据推测,在 SendAsync 完成之前我不应该调用 Dispose。但我应该称呼它吗(例如,使用“using”)。该场景是一个 WCF 服务,它在拨打电话时定期发送电子邮件。大多数计算速度很快,但发送电子邮件可能需要一秒钟左右,因此异步会更好。
我应该在每次发送邮件时创建一个新的 SmtpClient 吗?我应该为整个 WCF 创建一个吗?帮助!
更新以防万一,每封电子邮件都会根据用户进行定制。 WCF 托管在 Azure 上,Gmail 用作邮件程序。
I'm a bit perplexed on how to manage SmtpClient now that it is disposable, especially if I make calls using SendAsync. Presumably I should not call Dispose until SendAsync completes. But should I ever call it (e.g., using "using"). The scenario is a WCF service which mails out email periodically when calls are made. Most of the computation is fast, but the sending of email can take a second or so, so Async would be preferable.
Should I create a new SmtpClient each time I send mail? Should I create one for the entire WCF? Help!
Update In case it makes a difference, each email is always customized to the user. The WCF is hosted on Azure and Gmail is used as the mailer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
最初的问题是针对 .NET 4 提出的,但如果从 .NET 4.5 开始有帮助,SmtpClient 实现了异步等待方法
SendMailAsync
。因此,异步发送电子邮件如下:
最好避免使用 SendAsync 方法。
The original question was asked for .NET 4, but if it helps as of .NET 4.5 SmtpClient implements async awaitable method
SendMailAsync
.As a result, to send email asynchronously is as the following:
It's better to avoid using SendAsync method.
注意:.NET 4.5 SmtpClient 实现了
异步等待
方法SendMailAsync
。对于较低版本,请使用SendAsync
,如下所述。您应该始终尽早处置
IDisposable
实例。对于异步调用,这是在发送消息后的回调中。SendAsync
不接受回调,这有点烦人。Note: .NET 4.5 SmtpClient implements
async awaitable
methodSendMailAsync
. For lower versions, useSendAsync
as described below.You should always dispose of
IDisposable
instances at the earliest possibility. In the case of async calls, this is on the callback after the message is sent.It's a bit annoying the
SendAsync
doesn't accept a callback.一般来说,IDisposable对象应该尽快被释放;在对象上实现 IDisposable 的目的是传达这样一个事实:相关类拥有应确定性释放的昂贵资源。但是,如果创建这些资源的成本很高,并且您需要构造大量此类对象,那么将一个实例保留在内存中并重用它可能会更好(性能方面)。只有一种方法可以知道这是否有任何区别:分析它!
回复:处置和异步:显然你不能使用
using
。相反,您通常在 SendCompleted 事件中处置该对象:In general, IDisposable objects should be disposed as soon as possible; implementing IDisposable on an object is intended to communicate the fact that the class in question holds expensive resources that should be deterministically released. However, if creating those resources is expensive and you need to construct a lot of these objects, it may be better (performance wise) to keep one instance in memory and reuse it. There's only one way to know if that makes any difference: profile it!
Re: disposing and Async: you can't use
using
obviously. Instead you typically dispose the object in the SendCompleted event:好吧,我知道的老问题。但当我需要实现类似的东西时,我自己偶然发现了这一点。我只是想分享一些代码。
我正在迭代多个 SmtpClient 以异步发送多封邮件。我的解决方案与 TheCodeKing 类似,但我正在处理回调对象。我还将 MailMessage 作为 userToken 传递,以在 SendCompleted 事件中获取它,这样我也可以对其调用 dispose。像这样:
Ok, old question I know. But I stumbled upon this myself when I was in need of implementing something similar. I just wanted to share some code.
I'm iterating over several SmtpClients to send several mail asynchronously. My solution is similar to TheCodeKing, but I'm disposing the callback object instead. I'm also passing MailMessage as userToken to get it in the SendCompleted event so I can call dispose on that as well. Like this:
您可以通过以下评论了解为什么处置 SmtpClient 特别重要:
在我使用 Gmail 发送多封邮件而不处置客户端的场景中,我曾经得到:
You can see why it is particularly important to dispose of SmtpClient by the following comment:
In my scenario sending multiple mails using Gmail without disposing the client, I used to get:
我在asp.net 5.0 core中使用了这种方式。
I used this way in asp.net 5.0 core.