Tracing EWS requests by using the EWS Managed API 2.0

Last modified: December 10, 2012

Applies to: EWS Managed API | Exchange Server 2007 Service Pack 1 (SP1) | 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.

Debugging a Web service–based application can be difficult because part of the processing is performed on a computer to which you do not have access. Because you cannot step through the code on the server, it can be helpful to see the XML requests and responses that are exchanged between the client and the server to determine which part of the application is causing an error. When you are using the Microsoft Exchange Web Services (EWS) Managed API, you can use the tracing methods on the ExchangeService object to capture the XML request that is sent to Exchange Web Services and the response that the server returns to the application.

To enable tracing on the ExchangeService object

  1. Create an ExchangeService object for your application.

    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
    
  2. Set the tracing properties to enable tracing.

    service.TraceListener = ITraceListenerInstance;
    // Optional flags to indicate the requests and responses to trace.
    service.TraceFlags = TraceFlags.EwsRequest | TraceFlags.EwsResponse
    service.TraceEnabled = true;
    

After you set the TraceEnabled property to true, all requests that match the trace flags will be sent to the specified trace listener. You can specify a single trace flag, or you can specify multiple trace flags by combining them with a logical OR.

Note

You can also simply set the TraceEnabled property to true without implementing a TraceListener to output to the default output such as a console window.

Example

The following code example shows simple object that implements the ITraceListener interface and stores the traced requests and responses in XML or text files.

class TraceListener : ITraceListener
{
    #region ITraceListener Members

    public void Trace(string traceType, string traceMessage)
    {
      CreateXMLTextFile(traceType, traceMessage.ToString());
    }

    #endregion

    private void CreateXMLTextFile(string fileName, string traceContent)
    {
      // Create a new XML file for the trace information.
      try
      {
        // If the trace data is valid XML, create an XmlDocument object and save.
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(traceContent);
        xmlDoc.Save(fileName + ".xml");
      }
      catch
      {
        // If the trace data is not valid XML, save it as a text document.
        System.IO.File.WriteAllText(fileName + ".txt", traceContent);
      }
    }
}

Compiling the code

For information about compiling this code, see Getting started with the EWS Managed API 2.0.

Robust programming

  • Properly handle all errors.

  • Check response codes in the method result.

  • Check user input.

  • Use Autodiscover to determine the Web service end point.

Security

  • Use HTTP with SSL for all communications between client and server.

  • Protect user passwords.

  • Validate all user input.