I have to watch five calendars parallel if any notification appears. It works fine but anytime after a few notifications or a period of time I got this Exception “The Watermark is invalid”.
I have a list of mailboxes which I want to watch ( only the Calendar Folder). The trigger starts every few seconds and the method look whether there is a notification or not.
If I create a few appointments and anytime the exception “The Watermark is invalid” was thrown. It appears in the Line where i get the Events.
public Notification(ExchangeService _server1, string[] _mailboxName1)
{
_server = _server1;
_listOfList = _listOfList1;
_mailboxName = _mailboxName1;
foreach (Mailbox m in _mailboxName)
{
FolderId _Id = new FolderId(WellKnownFolderName.Calendar, m);
PullSubscription pullSub = _server.SubscribeToPullNotifications(new FolderId[] { _Id }, 5, null, EventType.Copied, EventType.Created, EventType.Deleted, EventType.Modified, EventType.Moved);
_subList.Add(pullSub);
}
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
AppSettingsReader config = new AppSettingsReader();
int time = Convert.ToInt16(config.GetValue("TimerInterval", typeof(string)));
aTimer.Interval = time;
aTimer.Enabled = false;
aTimer.Start();
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("Timer");
bool noteWatch = false;
foreach (PullSubscription p in _subList)
{
string type = null;
ItemId eventId = null;
//Exception the watermark is invalid!!
GetEventsResults events = p.GetEvents();
foreach (ItemEvent itemEvent in events.ItemEvents)
{
switch (itemEvent.EventType)
{
case EventType.Created:
noteWatch = true;
eventId = itemEvent.ItemId;
type = "Created";
break;
}
}
}
}
I found the reason:
My List of subscription was not thread safe.
I added this to my code and it works fine: