Dispose/Close ExchangeService in C#?

Simon Woker picture Simon Woker · Feb 9, 2012 · Viewed 8.8k times · Source

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();
    // ...
}

Answer

Jakob Christensen picture Jakob Christensen · Feb 11, 2012

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.