Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dispose/Close ExchangeService in C#?

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();
    // ...
}
like image 251
Simon Woker Avatar asked Feb 09 '12 11:02

Simon Woker


2 Answers

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.

like image 29
Seth Avatar answered Sep 28 '22 09:09

Seth


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.

like image 138
Jakob Christensen Avatar answered Sep 28 '22 11:09

Jakob Christensen