在 C# 中处置/关闭 ExchangeService?
我正在使用 ExchangeService WebService API (Microsoft.Exchange.WebServices.Data
),但找不到任何 Close
或 Dispose
方法。
是否不需要以某种方式关闭连接?
我的方法如下所示:
public void CheckMails()
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
IMAPCredentials creds = new IMAPCredentials();
service.Credentials = new NetworkCredential(creds.User, creds.Pass, creds.Domain);
service.AutodiscoverUrl(creds.User + "@example.com");
// not the real code from here on but you'll get the idea...
// var emails = service.FindItems();
// emails[0].Load();
// emails[0].Attachments[0].Load();
// ...
}
I'm using the ExchangeService WebService API (Microsoft.Exchange.WebServices.Data
) but I cannot find any Close
or Dispose
method.
Is it not neccessary to close the connection somehow?
My method looks like this:
public void CheckMails()
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
IMAPCredentials creds = new IMAPCredentials();
service.Credentials = new NetworkCredential(creds.User, creds.Pass, creds.Domain);
service.AutodiscoverUrl(creds.User + "@example.com");
// not the real code from here on but you'll get the idea...
// var emails = service.FindItems();
// emails[0].Load();
// emails[0].Attachments[0].Load();
// ...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ExchangeService
类上没有 Close/Dispose 方法,因为该类不维护与 Web 服务的连接。相反,会根据需要创建并关闭新的 HTTP 连接。例如,当您调用
ExchangeService.FindItems
时,将创建与 Exchange 服务器的新 HTTP 连接,并在对FindItems
的方法调用中关闭。There is no Close/Dispose method on the
ExchangeService
class because the class does not maintain a connection to the web services. Instead a new HTTP connection is created and closed as needed.For example when you call
ExchangeService.FindItems
a new HTTP connection to the Exchange server is created and closed within the method call toFindItems
.我意识到这已经很老了,但我最近也有同样的问题,因为我们在连接到邮箱后遇到了问题,并且不久之后再次尝试相同的方法,我们得到了 HTTP 异常。然后,等待一分钟左右,我们可以连接...但就像已接受答案的评论一样,这可能是 Exchange 服务器上的设置。
要回答这个问题,从技术上讲,由于 ExchangeService 没有实现 IDisposable,因此不需要 Dispose 连接,也不能将实例包装在 using 语句中。
I realize that this is pretty old, but I had the same question recently, because we've had a problem after connecting to a mailbox, and trying the same method again soon after, we get an HTTP exception. Then, after waiting a minute or so, we can connect...but like the comments on the accepted answer, this is probably a setting on the Exchange server.
To answer the question, technically speaking, since ExchangeService does not implement
IDisposable
, then there is no need to Dispose a connection, nor could you wrap an instance in ausing
statement.