Compartir a través de


Debugging Proxies in Exchange 2010

Last modified: October 16, 2009

Applies to: Exchange Server 2007 | Exchange Server 2010

Proxy-generated assemblies are created by means of either WSDL.exe or the Add Web Reference menu in MicrosoftVisual Studio 2005 or Microsoft Visual Studio 2008. The proxy assembly encapsulates the logic to create and send SOAP messages to and receive SOAP messages from the computer that is running Microsoft Exchange Server 2010 that has the Client Access server role installed. The SOAP messages define the contract between the client and server. This topic describes a technique that you can use to examine the XML messages that are built by the proxy classes.

The SOAP messages that are passed between the Exchange Web Services client and server are defined by the XML schema and WSDL files. The XML schema and WSDL files define the contract between the client and server. Proxy class generators create an object-model abstraction of those SOAP messages, which can simplify programming. The code example in this topic uses a proxy class library that was generated by Visual Studio 2005. Different proxy class generators create different object models for a given Web service. This proxy class code example is an illustration only; refer to the proxy class generator documentation for support for proxy classes.

To debug proxies by using the XmlSerializer class

  1. Create the Exchange Web Services proxy assembly by using WSDL.exe or the Add Web Reference menu in Visual Studio.

  2. Reference the proxy assembly by adding the using directive for C# or the Imports directive for MicrosoftVisual Basic .NET.

    using EWSClientProxyAssembly;
    
  3. Write the code to create a Web service request. The following code shows the formation of a FindItem Operation request.

    // Create the service binding.
    ExchangeServiceBinding esb = new ExchangeServiceBinding();
    esb.RequestServerVersionValue = new RequestServerVersion();
    esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2010;
    esb.Credentials = new NetworkCredential("UserName", "Password", "Domain");
    esb.Url = @"http://myexchangeserver.mydomain.com/EWS/Exchange.asmx";
    
    // Create the FindItem request.
    FindItemType findItemRequest = new FindItemType();
    findItemRequest.Traversal = ItemQueryTraversalType.Shallow;
    
    // Define which item properties are returned in the response.
    ItemResponseShapeType itemProperties = new ItemResponseShapeType();
    itemProperties.BaseShape = DefaultShapeNamesType.AllProperties;
    
    // Add properties shape to request.
    findItemRequest.ItemShape = itemProperties;
    
    // Identify which folders to search to find items.
    DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[2];
    folderIDArray[0] = new DistinguishedFolderIdType();
    folderIDArray[1] = new DistinguishedFolderIdType();
    folderIDArray[0].Id = DistinguishedFolderIdNameType.inbox;
    folderIDArray[1].Id = DistinguishedFolderIdNameType.drafts;
    
    // Add folders to request.
    findItemRequest.ParentFolderIds = folderIDArray;
    
    // Send the request and get the response.
    FindItemResponseType findItemResponse = esb.FindItem(findItemRequest);
    
  4. Create a static method that is used to create the XML messages from the requests and responses. The following example takes the request and response and creates a text file that includes an XML message.

    static void CreateXmlMessageTextFile(BaseRequestType request, BaseResponseMessageType response)
    {
      // Create a text file with the request XML.
      using (StreamWriter myReqWriter = new StreamWriter(request.GetType().Name + ".xml"))
      {
        XmlSerializer mySerializer = new XmlSerializer(request.GetType());
        mySerializer.Serialize(myReqWriter, request);
      }
    
      // Create a text file with the response XML.
      using (StreamWriter myRespWriter = new StreamWriter(response.GetType().Name + ".xml"))
      {
        XmlSerializer mySerializer = new XmlSerializer(response.GetType());
        mySerializer.Serialize(myRespWriter, response);
      }
    }
    
  5. Add a call to the CreateXmlMessageTextFile static method after the request has been sent and the response received.

    CreateXmlMessageTextFile(findItemRequest, findItemResponse);
    

    Note

    To view the entire example, see "Example" later in this topic.

  6. Build and run the code example. The CreateXmlMessageTextFile method will create text files that contain the XML messages. The text files that contain the XML messages can be used to debug the application. Use the WSDL.exe, schema files, and the Exchange Web Services Reference to review and verify the XML.

Example

The following example shows a FindItem Operation and how to view the XML sent by the FindItem SOAP messages.

static void FindItem()
{
   // Create the service binding.
   ExchangeServiceBinding esb = new ExchangeServiceBinding();
   esb.RequestServerVersionValue = new RequestServerVersion();
   esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2010;
   esb.Credentials = new NetworkCredential("UserName", "Password", "Domain");
   esb.Url = @"http://myexchangeserver.mydomain.com/EWS/Exchange.asmx";

   // Create the FindItem request.
   FindItemType findItemRequest = new FindItemType();
   findItemRequest.Traversal = ItemQueryTraversalType.Shallow;

   // Define which item properties are returned in the response.
   ItemResponseShapeType itemProperties = new ItemResponseShapeType();
   itemProperties.BaseShape = DefaultShapeNamesType.AllProperties;

   // Add properties shape to request.
   findItemRequest.ItemShape = itemProperties;

   // Identify which folders to search to find items.
   DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[2];
   folderIDArray[0] = new DistinguishedFolderIdType();
   folderIDArray[1] = new DistinguishedFolderIdType();
   folderIDArray[0].Id = DistinguishedFolderIdNameType.inbox;
   folderIDArray[1].Id = DistinguishedFolderIdNameType.drafts;

   // Add folders to request.
   findItemRequest.ParentFolderIds = folderIDArray;

   // Send the request and get the response.
   FindItemResponseType findItemResponse = esb.FindItem(findItemRequest);

   // Create XML message files.
   CreateXmlMessageTextFile(findItemRequest, findItemResponse);
}

static void CreateXmlMessageTextFile(BaseRequestType request, BaseResponseMessageType response)
{
  // Create a text file with the request XML.
  using (StreamWriter myReqWriter = new StreamWriter(request.GetType().Name + ".xml"))
  {
    XmlSerializer mySerializer = new XmlSerializer(request.GetType());
    mySerializer.Serialize(myReqWriter, request);
  }

  // Create a text file with the response XML.
  using (StreamWriter myRespWriter = new StreamWriter(response.GetType().Name + ".xml"))
  {
    XmlSerializer mySerializer = new XmlSerializer(response.GetType());
    mySerializer.Serialize(myRespWriter, response);
  }
}

See Also

Other Resources