未收到EWS流媒体通知

时间:2014-08-12 13:01:47

标签: c# exchangewebservices

下午所有,

我有一个Windows服务,订阅Office365电子邮件帐户并等待新电子邮件,当他们到达时处理他们的附件,并且一切都与世隔绝。

但是......出于某种原因,应用程序在一段不确定的时间后停止接收通知。

我已经处理了'OnDisconnect'事件并重新建立了连接,如下面的代码所示,但这似乎并没有解决这个问题。 Windows服务继续正常运行,如果我重新启动服务,一切都很好,直到再次失败。

这是我运行交换的课程:

 public class ExchangeConnection
    {
        static readonly ExchangeService Service = Exchange.Service.ConnectToService(UserDataFromConsole.GetUserData(), new TraceListener());
        public event EmailReceivedHandler OnEmailReceived;

        public ExchangeConnection()
        {
        }

        public void Open()
        {
            SetStreamingNotifications(Service);

            var signal = new AutoResetEvent(false);
            signal.WaitOne();
        }

        private void SetStreamingNotifications(ExchangeService service)
        {
            var streamingsubscription = service.SubscribeToStreamingNotifications(new FolderId[] { WellKnownFolderName.Inbox }, EventType.NewMail);

            var connection = new StreamingSubscriptionConnection(service, 30);

            connection.AddSubscription(streamingsubscription);
            connection.OnNotificationEvent += OnEvent;
            connection.OnSubscriptionError += OnError;
            connection.OnDisconnect += OnDisconnect;
            connection.Open();
        }

        public void MoveEmail(ItemId id, String folderName = "Archived Emails")
        {
            var rootFolder = Folder.Bind(Service, WellKnownFolderName.Inbox);

            var archivedFolder = rootFolder.FindFolders(new FolderView(100)).FirstOrDefault(x => x.DisplayName == folderName);

            if (archivedFolder == null)
            {
                archivedFolder = new Folder(Service) { DisplayName = folderName };
                archivedFolder.Save(WellKnownFolderName.Inbox);
            }

            Service.MoveItems(new List<ItemId> {id}, archivedFolder.Id);
        }

        #region events
        private void OnDisconnect(object sender, SubscriptionErrorEventArgs args)
        {
            //The connection is disconnected every 30minutes, and we are unable to override this,
            //so when we get disconnected we just need to reconnect again.
            var connection = (StreamingSubscriptionConnection)sender;

            connection.Open();
        }
        private void OnEvent(object sender, NotificationEventArgs args)
        {
            var subscription = args.Subscription;

            // Loop through all item-related events. 
            foreach (var notification in args.Events)
            {
                switch (notification.EventType)
                {
                    case EventType.NewMail:

                        if (notification is ItemEvent)
                        {
                            var email = Item.Bind(Service, new ItemId(((ItemEvent) notification).ItemId.UniqueId));
                            OnEmailReceived(new EmailReceivedArgs((EmailMessage)email));
                        }

                        break;
                }

            }
        }
        private void OnError(object sender, SubscriptionErrorEventArgs args)
        {
            var e = args.Exception;
            Logger.LogException(e,LogEventType.Error);
        }
        #endregion events

}

任何帮助都会很棒,谢谢。

编辑:

在改进错误记录后,我发现了这个异常:

Exception: The specified subscription was not found.

任何想法导致了什么?

1 个答案:

答案 0 :(得分:0)

使用Office365,您需要确保处理关联,请参阅http://msdn.microsoft.com/en-us/library/office/dn458789(v=exchg.150).aspx。添加这些标头将确保您的请求始终路由到正确的服务器。

干杯 格伦