未收到 EWS 流通知

问题描述 投票:0回答:1

我有一个 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.

你知道是什么原因造成的吗?

c# exchangewebservices
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.