我想跟蹤共享郵箱上的電子郵件活動。我的主要興趣是當郵件被移動,刪除和修改(類別變化)下面是我的申購代碼:EWS API連接意外關閉
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");
起初一切看上去一切正常。但是我注意到一些事件沒有被觸發。例如,如果用戶更改了電子郵件類別,然後立即移動它,則並非所有事件都得到處理。我看到我的控制檯上的輸出,並沒有看到「移動」事件。這會是什麼原因?
我建議你使用EWSeditor https://ewseditor.codeplex.com/(使用Streaming通知查看器)進行一些測試,並監聽所有事件,這些事件應該讓您更清楚地瞭解服務器通知的內容。 –
我會嘗試。我發現一些很有趣的東西。我注意到我的連接因「System.Net.Sockets.SocketException」而關閉。我嘗試過各種錯誤處理,但無法捕捉到異常。但是,通過將Outlook中的多個電子郵件從一個文件夾移動到另一個文件夾,我能夠導致連接出錯。我不知道爲什麼這樣的行爲會導致連接關閉。深入挖掘,我收到錯誤消息「操作超時」 – user1784014