订阅交换流通知时出现异常

时间:2015-10-20 18:37:44

标签: c# exchangewebservices ews-managed-api

我目前正在开发一个让我与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("username@example.com", 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: 

可以在https://github.com/OfficeDev/ews-managed-api/blob/master/Core/EwsXmlReader.cs

找到EwsXmlReader.cs的来源

看起来&#34;某些&#34; 正在预先 417 并将 2 附加到来自Exchange服务器的响应中。对我来说很明显为什么抛出异常,有文本数据不应该存在。对我来说不明显的是文本数据存在的原因。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

$json = '{"data": "0.7.4"}';
$array = json_decode($json, true);
var_dump($array);

这是这个难题的关键,你正在看到&#34; chunk&#34;。 417是删除漂亮打印格式时Transfer-Encoding: chunked 块长度的十六进制值。 2是最后一块,只是空白。分块传输格式为explained here

我重新格式化了XML以删除空白区域,您可以完全计算出0x417 = 1047个字符:

<Envelope>

显然,http转移应该删除它们,你的问题没有给出正确的引导,为什么这没有发生。但希望很好地找到根本原因。有趣的拼图顺便说一句:)