Setting Up the ExchangeServiceBinding Proxy Class
Topic Last Modified: 2009-07-15
The ExchangeServiceBinding proxy class contains the methods and properties that are used to send and receive the SOAP messages, set up Exchange impersonation, maintain user credentials, and identify the Exchange Web Services endpoint. When you are creating Exchange Web Services proxies, you must set up the ExchangeServiceBinding proxy class. This involves the following tasks:
- Setting the credentials of the user who is sending and receiving the XML requests and responses.
- Specifying the version of schema files that should be used to validate and process client requests.
- Setting the URL endpoint from which the Exchange service binding receives requests and sends responses.
- Identifying the operation that will process the requests.
The ExchangeServiceBinding class is created by Microsoft Visual Studio 2005 or Visual Studio 2008 when you create a Web reference from the Microsoft Exchange Server 2007 Exchange Web Services wsdl file.
Set the credentials of the user who sends requests to the Microsoft Exchange server. If you use the Add Web Reference user interface in Visual Studio 2005 or Visual Studio 2008 to create the proxy classes, you can use the default credentials of the authenticated user who is logged on to the application. To use the default user credentials, set the UseDefaultCredentials property on the ExchangeServiceBinding to true. If you are not using the default user credentials, use the Credentials property of the ExchangeServiceBinding class to explicitly set the credentials to a different user.
The UseDefaultCredentials property is not created for the ExchangeServiceBinding proxy class if you use wsdl.exe to generate the proxy classes. Use CredentialCache.DefaultCredentials to set the ExchangeServiceBinding to use the default user credentials.
Setting the Request Version
Setting the URL Endpoint
Set the URL of the Exchange Web Services endpoint on the URL property of the ExchangeServiceBinding class. This URL locates the exchange.asmx file on the Exchange server that has the Client Access server role installed.
You can use the Autodiscover service to query for the appropriate URL endpoint for the mailbox user if the URL is unknown. The URL that is returned in the ASUrl element in an Autodiscover response identifies the Exchange Web Services endpoint. For an example that shows how to use the Autodiscover service, see Autodiscover Sample Application.
Identifying the Operation That Will Process the Request
The ExchangeServiceBinding class contains a set of methods that correspond to the Web service methods that are implemented on the Exchange server. Each of these methods takes a particular corresponding type as an argument. The type that is provided as an argument has properties set on it that are serialized by the ExchangeServiceBinding into the XML payload that is sent to the Client Access server. For a list of operations that can be performed by using the Web service methods that are provided by the ExchangeServiceBinding, see Exchange Web Services Operations.
ExchangeServiceBinding Configuration Example
The following code example shows how to set up the ExchangeServiceBinding class to enable communication with the Client Access server that hosts Exchange Web Services.
01 ExchangeServiceBinding esb = new ExchangeServiceBinding(); 02 esb.RequestServerVersionValue = new RequestServerVersion(); 03 esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2007_SP1; 04 esb.Credentials = new NetworkCredential("username", "password", "domain"); 05 esb.Url = "https://CAS01.contoso.com/EWS/exchange.asmx";
This code example does the following:
Line 01 declares and instantiates an instance of the ExchangeServiceBinding class.
Use a single instance of the ExchangeServiceBinding class for each mailbox account that is accessed from an Exchange Web Services client application. The exception to this is when you use multiple threads to access the same mailbox. Then use multiple instances of the ExchangeServiceBinding for the multiple threads.
Line 02 initializes the RequestServerVersionValue property of the ExchangeServiceBinding class.
Line 03 specifies Exchange2007_SP1 as the version of schema files that the requests will target.
Line 04 sets the credentials of the user who sends requests to the Exchange server.
Line 05 sets the URL endpoint of Exchange Web Services. CAS01.contoso.com represents the fully qualified domain name (FQDN) of the Client Access server that provides client access for the mailbox user identified in Line 04.
After you set up the ExchangeServiceBinding proxy class, you must handle X509 certificates for SSL over HTTP.