EWS Best Practices: Use Autodiscover…

This is often overlooked but you should definitely use Autodiscover to get the EWS endpoint (Url) for each mailbox you are accessing.  Autodiscover is a process which involves sending requests to a few pre-determined URLs and optionally [edited 9/16/2011 by mstehle for clarification] some DNS and AD queries to identify the Autodiscover service endpoint and then calling the Autodiscover service to get the EWS endpoint.  The EWS Managed API wraps this process up neatly into a single method call which gives you no excuse not to use it.  Read more in the Autodiscover Best Practices documentation for the EWS Managed API on MSDN.

Typically an application will use Autodiscover on an initial request to a mailbox and then cache the URL for that mailbox because the Autodiscover process can take a while.  You definitely don’t want to do this before every request!  You also don’t want to cache the URL forever either.  It’s good practice to refresh the cached URL for a given mailbox every 24 hours or so and perform Autodiscover immediately following some error responses from EWS detailed here

“…if you are dealing with multiple mailboxes, you should call Autodiscover once for *each* mailbox you are trying to access. Of course, it is reasonable to cache this information on the client side. However, note that due to mailbox moves/migration, such cached information may end up getting stale, so don't hold onto it too long.

So when *should* you re-autodiscover? As a general practice, we would recommend you calling autodiscover once per day. In addition, if you encounter any of the following failure response codes, you should re-autodiscover:

  • ErrorMailboxMoveInProgress
  • ErrorMailboxStoreUnavailable
  • ErrorNoDestinationCASDueToKerberosRequirements
  • ErrorNoDestinationCASDueToSSLRequirements
  • ErrorNoDestinationCASDueToVersionMismatch
  • ErrorNoRespondingCASInDestinationSite
  • ErrorProxiedSubscriptionCallFailure
  • ErrorProxyCallFailed”

9/16/2011 - mstehle - Updated this list to reflect changes in Exchange 2010 and further research into the subject:

  • ErrorAutoDiscoverFailed
  • ErrorConnectionFailed
  • ErrorIncorrectSchemaVersion
  • ErrorInvalidCrossForestCredentials
  • ErrorInvalidIdReturnedByResolveNames
  • ErrorInvalidNetworkServiceContext
  • ErrorMailboxMoveInProgress
  • ErrorMailboxMoveInProgress
  • ErrorMailboxStoreUnavailable
  • ErrorNameResolutionNoMailbox
  • ErrorNameResolutionNoResults
  • ErrorNotEnoughMemory

Autodiscover is so important the Exchange team even wrote and recorded a song about it.