Search code examples
exchangewebservices

EWS API connection closed unexpectedly


I wanted to keep track of email activity on a shared mailbox. My primary interest is when email is moved,deleted, and modified(category changed) Below is my subscription code:

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();

Here's my event handler:

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");

At first everything seems to run fine. However I noticed some events didn't get triggered. For example, if the user changed email category and then moved it immediately, not all events get handled. I watched the output on my console and didn't see "moved" event. What would be the cause of this?


Solution

  • It turns out the problem has to do with EWS URL. I used EWS autodiscover. Googling online, I saw someone had similar issue. He/she resolved it by connecting to exchange server directly.

    I followed this document to find my Exchange service url.

    https://knowledge.kofax.com/MFD_Productivity/eCopy_ShareScan/Troubleshooting/Determining_the_Exchange_Web_Services_(EWS)_URL_for_the_ShareScan_Exchange_Connector

    EWS autodiscover used the url listed in "Protocol:Exchange RPC". I saw another url listed in "Protocal:Exchange HTTP". I manually set my URL to HTTP version. Issue resolved!!