EWS API连接意外关闭

时间:2016-02-29 16:53:50

标签: exchangewebservices

我想跟踪共享邮箱上的电子邮件活动。我的主要兴趣是移动,删除和修改电子邮件(更改类别)以下是我的订阅代码:

StreamingSubscription streamingsubscription = service.SubscribeToStreamingNotifications(
            GetFolders(),
            EventType.NewMail,
            EventType.Modified,//if the user modified the category
            EventType.Deleted,EventType.Moved);

        StreamingSubscriptionConnection connection = new StreamingSubscriptionConnection(service,30);

        connection.AddSubscription(streamingsubscription);
        // Delegate event handlers.
        connection.OnNotificationEvent +=
            new StreamingSubscriptionConnection.NotificationEventDelegate(OnEvent);
        connection.OnSubscriptionError +=
            new StreamingSubscriptionConnection.SubscriptionErrorDelegate(OnError);
        connection.OnDisconnect +=
            new StreamingSubscriptionConnection.SubscriptionErrorDelegate(OnDisconnect);
        connection.Open();

这是我的事件处理程序:

StreamingSubscription subscription = args.Subscription;
        var events = args.Events.Select(x => x.EventType.ToString()).ToArray();
        Console.WriteLine(Thread.CurrentThread.ManagedThreadId + "############Events Detected " + String.Join(",",events));



        foreach (NotificationEvent notification in args.Events)
        {
            if (notification is ItemEvent)
            {
                Console.WriteLine(Thread.CurrentThread.ManagedThreadId + "Handling Item Event");
                ItemEvent itemEvent;
                switch (notification.EventType)
                {

                    case EventType.NewMail:

                        itemEvent = (ItemEvent)notification;
                        Console.WriteLine(Thread.CurrentThread.ManagedThreadId + "-------------Mail Received:" + itemEvent.ItemId);
                        Save(itemEvent.ItemId, itemEvent.ParentFolderId.UniqueId);
                        break;
                    case EventType.Moved:

                        itemEvent = (ItemEvent)notification;
                        Console.WriteLine(Thread.CurrentThread.ManagedThreadId + "-------------Item Moved:" + itemEvent.OldItemId + " === " + itemEvent.ItemId);
                        Update(itemEvent.OldItemId, itemEvent.ItemId, itemEvent.ParentFolderId);

                        break;
                    case EventType.Deleted:

                        itemEvent = (ItemEvent)notification;
                        Console.WriteLine(Thread.CurrentThread.ManagedThreadId + "-------------Item deleted:" + itemEvent.ItemId);
                        Delete(itemEvent.ItemId);
                        break;
                    case EventType.Modified:

                        itemEvent = (ItemEvent)notification;
                        Console.WriteLine(Thread.CurrentThread.ManagedThreadId + "-------------Item Modified:" + itemEvent.ItemId);

                            Console.WriteLine(Thread.CurrentThread.ManagedThreadId + "-------------Item Changed Detected-----------");
                            Modify(itemEvent.ItemId);

                        break;
                }




            }
            else
            {
                Console.WriteLine(Thread.CurrentThread.ManagedThreadId + "------------Ignoring Folder Event");
            }

        }
        Console.WriteLine(Thread.CurrentThread.ManagedThreadId + "#######Done");

起初一切似乎都运行良好。但是我注意到有些事件没有被触发。例如,如果用户更改了电子邮件类别,然后立即将其移动,则不会处理所有事件。我在控制台上观看输出并没有看到“移动”事件。这是什么原因?

1 个答案:

答案 0 :(得分:0)

事实证明问题与EWS URL有关。我使用了EWS自动发现。谷歌在线搜索,我看到有人有类似的问题。他/她通过直接连接到Exchange服务器解决了这个问题。

我按照此文档查找了我的Exchange服务网址。

http://nuanceimaging.custhelp.com/app/answers/detail/a_id/13098/~/determining-the-exchange-web-services-(ews)-url-for-the-sharescan-exchange

EWS自动发现使用了"协议:Exchange RPC"中列出的网址。我在" Protocal:Exchange HTTP"中看到了另一个网址。我手动将我的URL设置为HTTP版本。问题已解决!!