I have written a listener class which creates an HTTP listener and subscribes its URL to push subscriptions (on new mail event on Inbox using EWS Managed API), receives notifications, processes mails and then move those mails to Deleted Items folder. I have also written a poller which periodically checks for any mails left out inside the inbox, processes them and then moves them to the Deleted Items folder. Listener and poller run in different threads.
I have synchronized these threads using locks. And the lock functionality is tested to work fine. However due to locking I am getting some undesired behavior.
Consider listener receives a push notification. It starts processing it. Meantime, poller starts and it acquires the lock. Now when listener tries to acquire the lock it finds that the lock is already acquired by the poller. So it waits till lock gets freed. Say listener executes till instruction INSTR-1
and continue waiting. Now poller is processing all mails in inbox for say 3-5 minutes. After that, poller releases the lock. In those 3-5 minutes, exchange continues to dispatch push notifications, however since first listener thread is stuck at INSTR-1
no more listener threads are dispatched by .NET runtime. But as soon as poller releases lock, all threads gets dispatched. But since their corresponding mails have been already processed and moved to Deleted Items folder by the poller, those listener threads throws the exception “The specified object was not found in the store.” on EmailMessage.Bind()
method. Since I have now well analyzed this behavior and I know exactly why this exception is occuring, I can simply go on to suppress these exceptions (say with empty catch
blocks).
However I will like to know,
if there is any way by which I can make Exchange Server to not to dispatch any push notifications after poller starts or in other words for those mails which have been already retrieved by EWS Managed API call
ExchangeService.FindItems()
made by poller.if there is any standard / Microsoft recommended way to handle such problems
We can visualize the listener and poller procedures and execution behavior as follows: