Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27173

VS 2010 [RESOLVED] InvalidCastException but is it?

$
0
0
I am trying to raise an event upon detection in changes to my EWS PullSubscription.

This is my code.


vbNET Code:
  1. Public Delegate Sub pullSubscriptionEventHandler(ByVal subscription As pullsubscription, ByVal folderEvent As folderEvent, ByVal itemEvents() As IEnumerable(Of ItemEvent), ByVal _emailService As ExchangeService)
  2.  
  3.  
  4. Private Shared _events As EventHandlerList = Nothing
  5.  
  6.     Public Shared ReadOnly Property Events() As EventHandlerList
  7.         Get
  8.             If _events Is Nothing Then
  9.                 _events = New EventHandlerList()
  10.             End If
  11.             Return _events
  12.         End Get
  13.     End Property
  14.  
  15. Public Custom Event PullSubscriptionChange As pullSubscriptionEventHandler
  16.         AddHandler(ByVal value As pullSubscriptionEventHandler)
  17.             Events.AddHandler("PullSubscriptionChangeEvent", value)
  18.         End AddHandler
  19.  
  20.         RemoveHandler(ByVal value As pullSubscriptionEventHandler)
  21.             Events.RemoveHandler("PullSubscriptionChangeEvent", value)
  22.         End RemoveHandler
  23.  
  24.         RaiseEvent(ByVal subscription As PullSubscription, ByVal folderEvent As FolderEvent, ByVal itemEvents() As IEnumerable(Of ItemEvent), ByVal _emailService As ExchangeService)
  25.             CType(Events("PullSubscriptionChangeEvent"), pullSubscriptionEventHandler).Invoke(subscription, folderEvent, itemEvents, _emailService)
  26.         End RaiseEvent
  27.     End Event
  28.  
  29.  
  30.  
  31. Public Sub New()
  32.      AddHandler PullSubscriptionChange, New pullSubscriptionEventHandler(AddressOf Me._emailWatcher.OnPullSubscriptionChangeEvent)
  33. End Sub
  34.  
  35.  
  36.  
  37.  Private Sub OnTimedEvent(ByVal source As Object, ByVal e As ElapsedEventArgs)
  38.         Dim notificationEvents As GetEventsResults = _subscription.GetEvents()
  39.  
  40.         If notificationEvents.AllEvents.Count > 0 Then
  41.  
  42.             Dim folderEvent As IEnumerable(Of FolderEvent) = notificationEvents.FolderEvents.Select(Function(p) p).Where(Function(p) p.FolderId.Equals(_trackFolderId)).ToList
  43.             Dim itemEvents As IEnumerable(Of ItemEvent) = notificationEvents.ItemEvents.Select(Function(p) p).Where(Function(p) p.ParentFolderId.Equals(_trackFolderId)).ToList
  44.  
  45.             Dim signalChange As Boolean = folderEvent.Count > 0
  46.  
  47.             If signalChange Then
  48.  
  49.                 Try
  50.  
  51.                     RaiseEvent PullSubscriptionChange(_subscription, folderEvent(0), itemEvents, _emailService)
  52.  
  53.                 Catch ex As InvalidCastException
  54.  
  55.                     Debug.Print("Failed to cast itemEvents.")
  56.  
  57.                 End Try
  58.  
  59.             End If
  60.         Else
  61.             Console.WriteLine("No changes have been detected.")
  62.         End If
  63.  
  64.     End Sub
  65.  
  66. Public Sub OnPullSubscriptionChangeEvent(ByVal subscription As PullSubscription, ByVal folderEvent As FolderEvent, ByVal itemEvents() As IEnumerable(Of ItemEvent), Optional ByVal emailService As ExchangeService = Nothing)
  67.         Dim handler As pullSubscriptionEventHandler = CType(MxsCacheDependency.Events("PullSubscriptionChangeEvent"), pullSubscriptionEventHandler)
  68.         If (handler IsNot Nothing) Then
  69.             Console.WriteLine("OnPullSubscriptionChangeEvent was fired with:" & vbCrLf & "{0} Subscription ID: {1}" _
  70.                               & vbCrLf & " EventType: {2}" & vbCrLf & "on Thread: {3}.", _
  71.                               DateTime.Now.ToString("h:mm:ss.fff"), subscription.Id, folderEvent.EventType, _
  72.                               Threading.Thread.CurrentThread.ManagedThreadId)
  73.  
  74.             _pullSubscription = subscription
  75.             _folderEvent = folderEvent
  76.             _itemEvents = itemEvents
  77.             If Not emailService Is Nothing Then
  78.                 _emailService = emailService
  79.             End If
  80.  
  81.             GetItemChanges()
  82.  
  83.         End If
  84.     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:
  1. 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.

Viewing all articles
Browse latest Browse all 27173

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>