20

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();
    // ...
}
Simon Woker
  • 4,994
  • 1
  • 27
  • 41

3 Answers3

26

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 to FindItems.

Jakob Christensen
  • 14,826
  • 2
  • 51
  • 81
  • You still need to close WCF-services and there's a nasty bug with wrapping them in a `using`, that's why I ask. Thanks for the explanation! – Simon Woker Feb 12 '12 at 18:10
  • 1
    @SimonWoker: I have been wondering myself how the ExchangeService class works with respect to Close/Dispose so that is why I have been digging into it. Note that EWS is not a WCF service as it has been implemented the "old fashioned" ASMX way. – Jakob Christensen Feb 12 '12 at 22:47
  • @JakobChristensen: When we try to connect to EWS(using the same credentials) multiple times, after 5 times we are getting permission denied error. Does that mean, EWS holds the connection for defined time period and doesn't allow any more connections? – Naresh Feb 27 '14 at 06:55
  • @Naresh: I am pretty sure that the EWS Managed API allows you to make as many calls as you want. As I wrote in my answer above, the EWS Managed API does not maintain connections. I know very little about setting up an Exchange server but I believe what you are experiencing has to do with EWS throttling on the Exchange server. I am sorry that I cannot give you a better answer. Maybe this article can be of help to you: http://msdn.microsoft.com/en-us/library/office/jj945066(v=exchg.150).aspx – Jakob Christensen Feb 27 '14 at 08:24
  • One issue I'm finding with ExchangeService is connections are left in a TIME_WAIT state, which by default will be for 4 minutes. If you poll Exchange frequently then this becomes a problem with port exhaustion. Although the ideal solution is to use subscriptions which maintains one open connection. – tjmoore Dec 16 '16 at 16:12
2

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 a using statement.

Jakob Christensen
  • 14,826
  • 2
  • 51
  • 81
Seth
  • 342
  • 1
  • 4
  • 14
0
private static void ProcessMail()
{
    ExchangeService exchange = new ExchangeService();
    exchange.Credentials = new WebCredentials(sACCOUNT, sPASSWORD, sDOMAIN);
    exchange.AutodiscoverUrl(sEMAIL_ADDRESS);

    if (exchange != null)
    {
        Folder rootFolder = Folder.Bind(exchange, WellKnownFolderName.Inbox);
        rootFolder.Load();

        foreach (Folder folder in rootFolder.FindFolders(new FolderView(100)))
        {
            //your code
        }
        exchange = null;
    }
}