I am trying to raise an event upon detection in changes to my EWS PullSubscription.
This is my code.
I am just confused, because everything was working prior to me adding everything for the event for ItemEvents. After adding it, I get the InvalidCastException when I RaiseEvent.
The line above populates just fine so I would think it would pass through just fine.
This is my code.
vbNET Code:
Public Delegate Sub pullSubscriptionEventHandler(ByVal subscription As pullsubscription, ByVal folderEvent As folderEvent, ByVal itemEvents() As IEnumerable(Of ItemEvent), ByVal _emailService As ExchangeService) Private Shared _events As EventHandlerList = Nothing Public Shared ReadOnly Property Events() As EventHandlerList Get If _events Is Nothing Then _events = New EventHandlerList() End If Return _events End Get End Property Public Custom Event PullSubscriptionChange As pullSubscriptionEventHandler AddHandler(ByVal value As pullSubscriptionEventHandler) Events.AddHandler("PullSubscriptionChangeEvent", value) End AddHandler RemoveHandler(ByVal value As pullSubscriptionEventHandler) Events.RemoveHandler("PullSubscriptionChangeEvent", value) End RemoveHandler RaiseEvent(ByVal subscription As PullSubscription, ByVal folderEvent As FolderEvent, ByVal itemEvents() As IEnumerable(Of ItemEvent), ByVal _emailService As ExchangeService) CType(Events("PullSubscriptionChangeEvent"), pullSubscriptionEventHandler).Invoke(subscription, folderEvent, itemEvents, _emailService) End RaiseEvent End Event Public Sub New() AddHandler PullSubscriptionChange, New pullSubscriptionEventHandler(AddressOf Me._emailWatcher.OnPullSubscriptionChangeEvent) End Sub Private Sub OnTimedEvent(ByVal source As Object, ByVal e As ElapsedEventArgs) Dim notificationEvents As GetEventsResults = _subscription.GetEvents() If notificationEvents.AllEvents.Count > 0 Then Dim folderEvent As IEnumerable(Of FolderEvent) = notificationEvents.FolderEvents.Select(Function(p) p).Where(Function(p) p.FolderId.Equals(_trackFolderId)).ToList Dim itemEvents As IEnumerable(Of ItemEvent) = notificationEvents.ItemEvents.Select(Function(p) p).Where(Function(p) p.ParentFolderId.Equals(_trackFolderId)).ToList Dim signalChange As Boolean = folderEvent.Count > 0 If signalChange Then Try RaiseEvent PullSubscriptionChange(_subscription, folderEvent(0), itemEvents, _emailService) Catch ex As InvalidCastException End Try End If Else Console.WriteLine("No changes have been detected.") End If End Sub Public Sub OnPullSubscriptionChangeEvent(ByVal subscription As PullSubscription, ByVal folderEvent As FolderEvent, ByVal itemEvents() As IEnumerable(Of ItemEvent), Optional ByVal emailService As ExchangeService = Nothing) Dim handler As pullSubscriptionEventHandler = CType(MxsCacheDependency.Events("PullSubscriptionChangeEvent"), pullSubscriptionEventHandler) If (handler IsNot Nothing) Then Console.WriteLine("OnPullSubscriptionChangeEvent was fired with:" & vbCrLf & "{0} Subscription ID: {1}" _ & vbCrLf & " EventType: {2}" & vbCrLf & "on Thread: {3}.", _ DateTime.Now.ToString("h:mm:ss.fff"), subscription.Id, folderEvent.EventType, _ Threading.Thread.CurrentThread.ManagedThreadId) _pullSubscription = subscription _folderEvent = folderEvent _itemEvents = itemEvents If Not emailService Is Nothing Then _emailService = emailService End If GetItemChanges() End If End Sub
I am just confused, because everything was working prior to me adding everything for the event for ItemEvents. After adding it, I get the InvalidCastException when I RaiseEvent.
vbNET Code:
Dim itemEvents As IEnumerable(Of ItemEvent) = notificationEvents.ItemEvents.Select(Function(p) p).Where(Function(p) p.ParentFolderId.Equals(_trackFolderId)).ToList
The line above populates just fine so I would think it would pass through just fine.