2015-10-20 20 views
3

我目前正在從事一項與Exchange集成的項目。其中一個要求是監視新郵件的郵箱,我認爲利用通知將是一個好主意。訂閱交換流式通知時出現異常

我寫了一個示例應用程序來熟悉如何利用流媒體的通知,但我遇到了以下錯誤:The expected XML node type was Element, but the actual type is Text.

下面是我寫的示例應用程序的源:

using Microsoft.Exchange.WebServices.Data; 
using System; 
using System.Net; 

namespace ExampleProgram 
{ 
    class Program 
    { 
     public static StreamingSubscriptionConnection streamingConnection; 

     public static bool RedirectionUrlValidationCallback(string redirectionUrl) 
     { 
      bool result = false; 

      Uri redirectionUri = new Uri(redirectionUrl); 

      if (redirectionUri.Scheme == "https") 
      { 
       result = true; 
      } 

      return result; 
     } 

     public static void NewMailSubscriptionDisconnect(object sender, SubscriptionErrorEventArgs args) 
     { 
      Exception e = args.Exception; 
      Console.Write("Disconnect: "); 
      Console.WriteLine(e.Message); 

      if (streamingConnection != null && !streamingConnection.IsOpen) 
      { 
       streamingConnection.Open(); 
      } 
     } 

     public static void NewMailSubscriptionError(object sender, SubscriptionErrorEventArgs args) 
     { 
      Exception e = args.Exception; 
      Console.Write("Disconnect: "); 
      Console.WriteLine(e.Message); 
     } 

     public static void NewMailSubscriptionNotification(object sender, NotificationEventArgs args) 
     { 
      Console.WriteLine("New message has arrived"); 
     } 

     static void Main(string[] args) 
     { 
      var exchangeService = new ExchangeService(ExchangeVersion.Exchange2013_SP1); 

      exchangeService.Credentials = new NetworkCredential("username", "password", "domain"); 
      exchangeService.TraceEnabled = true; 
      exchangeService.TraceFlags = TraceFlags.All; 
      exchangeService.TraceEnablePrettyPrinting = true; 
      exchangeService.AutodiscoverUrl("[email protected]", RedirectionUrlValidationCallback); 

      var newMailSubscription = exchangeService.SubscribeToStreamingNotificationsOnAllFolders(EventType.NewMail); 

      streamingConnection = new StreamingSubscriptionConnection(exchangeService, 30); 
      streamingConnection.AddSubscription(newMailSubscription); 
      streamingConnection.OnNotificationEvent += new StreamingSubscriptionConnection.NotificationEventDelegate(NewMailSubscriptionNotification); 
      streamingConnection.OnSubscriptionError += new StreamingSubscriptionConnection.SubscriptionErrorDelegate(NewMailSubscriptionError); 
      streamingConnection.OnDisconnect += new StreamingSubscriptionConnection.SubscriptionErrorDelegate(NewMailSubscriptionDisconnect); 
      streamingConnection.Open(); 

      do { } while (Console.ReadKey(true).Key != ConsoleKey.Escape); 
     } 
    } 
} 

從上面的源代碼可以看出,我打開了跟蹤。以下就是從這些痕跡產生:

EwsResponseHttpHeader

<Trace Tag="EwsResponseHttpHeaders" Tid="17" Time="2015-10-20 17:42:31Z"> 
    HTTP/1.1 200 OK 
    Transfer-Encoding: chunked 
    request-id: <redacted> 
    X-CalculatedBETarget: EXAMPLE-EXCHANGE-01.example.com 
    X-NoBuffering: 1 
    X-DiagInfo: EXAMPLE-EXCHANGE-01 
    X-BEServer: EXAMPLE-EXCHANGE-01 
    Cache-Control: private 
    Set-Cookie: exchangecookie=<redacted>; path=/,X-BackEndCookie=<redacted>; expires=Thu, 19-Nov-2015 17:42:30 GMT; path=/ews; secure; HttpOnly 
    Server: Microsoft-IIS/8.5 
    X-AspNet-Version: 4.0.30319 
    Persistent-Auth: true 
    X-Powered-By: ASP.NET 
    X-FEServer: EXAMPLE-EXCHANGE-02 
    Date: Tue, 20 Oct 2015 17:42:30 GMT 
</Trace> 

EwsResponse

<Trace Tag="EwsResponse" 
     Tid="15" 
     Time="2015-10-20 16:52:07Z" 
     Version="0.0.0.0"> 

    417 <!-- What is this? --> 

    <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"> 

     <soap11:Header xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/"> 
      <ServerVersionInfo xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           MajorVersion="15" 
           MinorVersion="0" 
           MajorBuildNumber="1130" 
           MinorBuildNumber="6" 
           Version="V2_23" 
           xmlns="http://schemas.microsoft.com/exchange/services/2006/types" /> 
     </soap11:Header> 

     <soap11:Body xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/"> 
      <m:GetStreamingEventsResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
              xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" 
              xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"> 
       <m:ResponseMessages> 
        <m:GetStreamingEventsResponseMessage ResponseClass="Success"> 
         <m:ResponseCode>NoError</m:ResponseCode> 
         <m:ConnectionStatus>OK</m:ConnectionStatus> 
        </m:GetStreamingEventsResponseMessage> 
       </m:ResponseMessages> 
      </m:GetStreamingEventsResponse> 

     </soap11:Body> 

    </Envelope> 

    2 <!-- Not sure what this is either... --> 

</Trace> 

異常詳細信息

Microsoft.Exchange.WebServices.Data.ServiceXmlDeserializationException occurred 
    HResult=-2146233088 
    Message=The expected XML node type was Element, but the actual type is Text. 
    Source=Microsoft.Exchange.WebServices 
    StackTrace: 
     at Microsoft.Exchange.WebServices.Data.EwsXmlReader.Read(XmlNodeType nodeType) in C:\Projects\ews-managed-api\Core\EwsXmlReader.cs:line 187 
    InnerException: 

它看起來像「東西」在前面加上和附加從Exchange服務器的響應:

EwsXmlReader.cs源可以在這裏找到。這很明顯爲什麼拋出異常,文本數據不應該存在。對我來說不明顯的是爲什麼那些文本數據在那裏。

任何想法?

+0

什麼是異常的堆棧跟蹤? –

+0

數字417和2總是相同的?它可能是一種來自別的東西的「BOF」和「EOF」嗎? –

+0

@YacoubMassad我添加了異常細節並引用了異常的來源。 – Tombatron

回答

1
Transfer-Encoding: chunked 

這是這個難題的關鍵,你看到的「塊」。 417是一個十六進制值,用於刪除漂亮打印格式時<Envelope>塊的長度。 2是最後的塊,只是空白。分塊傳輸格式爲explained here

我重新格式化XML除去白色空間,可以算過究竟0x417 = 1047字符:

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><soap11:Header xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/"><ServerVersionInfo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" MajorVersion="15" MinorVersion="0" MajorBuildNumber="1130" MinorBuildNumber="6" Version="V2_23" xmlns="http://schemas.microsoft.com/exchange/services/2006/types"/></soap11:Header><soap11:Body xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/"><m:GetStreamingEventsResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"><m:ResponseMessages><m:GetStreamingEventsResponseMessage ResponseClass="Success"><m:ResponseCode>NoError</m:ResponseCode><m:ConnectionStatus>OK</m:ConnectionStatus></m:GetStreamingEventsResponseMessage></m:ResponseMessages></m:GetStreamingEventsResponse></soap11:Body></Envelope> 

顯然HTTP傳輸應該刪除它們,你的問題沒有給出像樣的領導爲什麼這樣做不會發生。但希望有一個相當不錯的領導者找到根本原因。有趣的益智btw :)

+0

當然希望我知道我的環境造成了什麼。但我會打電話給這個答案。 – Tombatron