Partager via


Autodiscover best practices

Last modified: October 13, 2012

Applies to: EWS Managed API | Exchange Server 2010

Note: This content applies to the EWS Managed API 2.0 and earlier versions. For the latest information about the EWS Managed API, see Web services in Exchange.

In this article
Use the Autodiscover service to locate the service endpoint
Use a Try/Catch block
Get only the necessary user settings

The Autodiscover service enables you to retrieve configuration information for any user in your domain. This topic provides some best practices for working with the Autodiscover service.

Use the Autodiscover service to locate the service endpoint

If the URL of the Exchange Web Services (EWS) endpoint is already known, you do not need to use the Autodiscover service to make the connection. However, you can set the URL manually or use the best endpoint for a given user. Because the location of the best endpoint for a given user can change, for example when a new Client Access server is deployed, or can vary depending on the user, we recommend that you use the Autodiscover service.

The following example shows how to manually set the URL of the EWS endpoint.

service.Url = new Uri("https://mail.Contoso.com/EWS/Exchange.asmx");
service.Url = New Uri("https://mail.Contoso.com/EWS/Exchange.asmx")

The following example shows how to use the Autodiscover service to determine the best endpoint that is closest to the user's mailbox server.

service.AutodiscoverUrl("User1@Contoso.com");
service.AutodiscoverUrl("User1@Contoso.com")

Use a Try/Catch block

If you try to perform an autodiscovery on a nonexistent e-mail address, your attempt will result in an error. To prevent an invalid address from causing an application failure, we recommend that you include the AutodiscoverUrl call in a Try/Catch block. The following example shows how to use a Try/Catch block to handle the exception and to display the error message. When the e-mail address does not exist in the domain, an exception is thrown and the following message is displayed:

The e-mail address cannot be found.

// Set the URL.
try
{
    service.AutodiscoverUrl("User1_with_a_non_existent_name@Contoso.com");
}
catch (AutodiscoverRemoteException ex)
{
    Console.WriteLine("Exception thrown: " + ex.Error.Message);
}
' Set the URL.
Try
    service.AutodiscoverUrl("User1_with_a_non_existent_name@Contoso.com")
Catch ex As AutodiscoverRemoteException
    Console.WriteLine("Exception thrown: " + ex.Error.Message)
End Try

Get only the necessary user settings

The Autodiscover service enables client applications to retrieve many configuration settings. However, this consumes bandwidth and possibly exposes the configuration settings. We recommend that you use the GetUserSettings(String, []) method to retrieve only the specific configuration settings that are required. In the following example, only four settings are requested: UserDisplayName, InternalMailboxServer, PublicFolderServer, and ExternalMailboxServer.

GetUserSettingsResponse userresponse = autodiscoverService.GetUserSettings(
    "User1@Contoso.com",
    UserSettingName.UserDisplayName,
    UserSettingName.InternalMailboxServer,
    UserSettingName.PublicFolderServer,
    UserSettingName.ExternalMailboxServer);
Dim userresponse As GetUserSettingsResponse
userresponse = autodiscoverService.GetUserSettings( _
    "User1@Contoso.com", _
    UserSettingName.UserDisplayName, _
    UserSettingName.InternalMailboxServer, _
    UserSettingName.PublicFolderServer, _
    UserSettingName.ExternalMailboxServer)