private void btnStreamUnsubscribe_Click(object sender, RoutedEventArgs e) { _streamSubscription.Unsubscribe(); txtSubscriptionActivity.Text += "Stream Subscription Unsubscribed" + Environment.NewLine; btnStreamSubscribe.IsEnabled = true; btnStreamUnsubscribe.IsEnabled = false; }
// When the subscription connection expires, determine whether the subscription should be kept open. static private void OnDisconnect(object sender, SubscriptionErrorEventArgs args) { // Cast the sender as a StreamingSubscriptionConnection object. StreamingSubscriptionConnection connection = (StreamingSubscriptionConnection)sender; // Ask the user if they want to reconnect or close the subscription. ConsoleKeyInfo cki; Console.WriteLine("The StreamingSubscriptionConnection has expired."); Console.WriteLine("\r\n"); Console.WriteLine("Do you want to reconnect to the subscription? Enter Y or N"); Console.WriteLine("\r\n"); while (true) { cki = Console.ReadKey(true); { if (cki.Key == ConsoleKey.Y) { connection.Open(); Console.WriteLine("Connection open."); Console.WriteLine("\r\n"); break; } else if (cki.Key == ConsoleKey.N) { Signal.Set(); // Unsubscribe from the notification subscription. StreamingSubscription.Unsubscribe(); // Close the connection. connection.Close(); break; } } } }
private void UnsubscribeAll() { // Unsubscribe all if (_subscriptions == null) { return; } for (int i = _subscriptions.Keys.Count - 1; i >= 0; i--) { string sMailbox = _subscriptions.Keys.ElementAt <string>(i); StreamingSubscription subscription = _subscriptions[sMailbox]; try { subscription.Unsubscribe(); _logger.Log(String.Format("Unsubscribed from {0}", sMailbox)); } catch (Exception ex) { _logger.Log(String.Format("Error when unsubscribing {0}: {1}", sMailbox, ex.Message)); } _subscriptions.Remove(sMailbox); } }
////DJB //private void GetGroupingInfo(ExchangeService oExchangeService, string DiscoverMailbox) //{ // //oExchangeService.get // string UseVersion = oExchangeService.RequestedServerVersion.ToString(); // string UseUrl = oExchangeService.Url.ToString(); // djb - need to fix it to the autodiscover url. // string EwsRequest = EwsRequests.GroupingInformationTemplateRequest; // EwsRequest = EwsRequest.Replace("XXXExchangeVersionXXX", UseVersion); // EwsRequest = EwsRequest.Replace("XXXAutoDiscoverServiceServerXXX", UseUrl); // EwsRequest = EwsRequest.Replace("XXXMailboxSmtpXXX", DiscoverMailbox); // // DO raw post // // string sResponse // // if (DoRawPost(UseUrl, oExchangeService.ServerCredentials, EwsRequest, ref sResponse)); // // { // // Extract GroupingInformation and external url from sResponse); // // } // // else // // { // // return error; // // } //} private void StreamingSubscribeWork() { // djb // get a list of maiboxes, thier external ews urls and grouping info // list of items = GetGroupingInfo(ExchangeService oExchangeService, string DiscoverMailbox); // Sort list of items by url + GroupingInformaiton // try { lock (WorkThreads) { WorkThreads.Add(Thread.CurrentThread); SetControlText(ThreadCount, WorkThreads.Count.ToString()); } if (chkSerialize.Checked) { mutConnection.WaitOne(); } string TID = Thread.CurrentThread.ManagedThreadId.ToString("[0]"); ListViewItem item = new ListViewItem(); item.Tag = "[local]ThreadStart"; item.Text = "[local]ThreadStart"; item.SubItems.Add(TID); item.SubItems.Add(DateTime.Now.ToString()); AddToDisplay(lstEvents, item); // Note that EWS Managed API objects should not beshared accross threads - So, each thread should have its own service object. //EWSEditor.Common.EwsEditorServiceInstanceSettings.EwsEditorAppSettings oSettings = new EWSEditor.Common.EwsEditorServiceInstanceSettings.EwsEditorAppSettings(); EWSEditor.Common.EwsEditorAppSettings oCurrentAppSettings = null; ExchangeService ThreadLocalService = EwsProxyFactory.CreateExchangeService( ); // Todo: Flush out oCurrentAppSettings CurrentAppSettings = oCurrentAppSettings; List <StreamingSubscription> ThreadLocalSubscriptions = new List <StreamingSubscription>(); // Create the subscriptions based on the form settings for (int i = 0; i < numSubs.Value; i++) { try { StreamingSubscription CurrentSubscription = null; if (chkAllFoldes.Checked == false) { CurrentSubscription = ThreadLocalService.SubscribeToStreamingNotifications( new FolderId[] { this.CurrentFolderId }, EventTypes.ToArray()); //System.Diagnostics.Debug.WriteLine("-"); //System.Diagnostics.Debug.WriteLine("Subscribe - ID: " + CurrentSubscription.Id + " Watermark: " + CurrentSubscription.Watermark); //System.Diagnostics.Debug.WriteLine("-"); } else { CurrentSubscription = ThreadLocalService.SubscribeToStreamingNotificationsOnAllFolders( EventTypes.ToArray()); //System.Diagnostics.Debug.WriteLine("-"); //System.Diagnostics.Debug.WriteLine("Subscribe - ID: " + CurrentSubscription.Id + " Watermark: " + CurrentSubscription.Watermark); //System.Diagnostics.Debug.WriteLine("-"); } ThreadLocalSubscriptions.Add(CurrentSubscription); lock (ActiveSubscriptions) { ActiveSubscriptions.Add(CurrentSubscription); SetControlText(SubscriptionCount, ActiveSubscriptions.Count.ToString()); } } catch (Exception ex) { DebugLog.WriteException("Error Subscribe or Add [TID:" + TID + "]", ex); item = new ListViewItem(); item.Tag = "[local]SubscribeError"; item.Text = "[local]SubscribeError"; item.ToolTipText = ex.ToString(); item.SubItems.Add(TID); item.SubItems.Add(DateTime.Now.ToString()); AddToDisplay(lstEvents, item); } } // Create a new StreamingSubscriptionConnection StreamingSubscriptionConnection CurrentConnection = new StreamingSubscriptionConnection(ThreadLocalService, (int)SubscriptionLifetime.Value); lock (ActiveConnections) { ActiveConnections.Add(CurrentConnection); SetControlText(ConnectionCount, ActiveConnections.Count.ToString()); } // Add Handlers CurrentConnection.OnDisconnect += OnDisconnect; CurrentConnection.OnSubscriptionError += OnSubscriptionError; CurrentConnection.OnNotificationEvent += OnStreamingEvent; // Add the Subscriptions to the Connection foreach (StreamingSubscription CurrentSubscription in ThreadLocalSubscriptions) { CurrentConnection.AddSubscription(CurrentSubscription); } if (chkSerialize.Checked) { mutConnection.ReleaseMutex(); } // Open the Connection try { if (ThreadLocalService.CookieContainer.Count > 0) { System.Net.CookieCollection MyCookies = ThreadLocalService.CookieContainer.GetCookies(ThreadLocalService.Url); } CurrentConnection.Open(); ShutdownThreads.WaitOne(); } catch (Exception ex) { DebugLog.WriteException("Error Opening StreamingSubscriptionConnection [TID:" + Thread.CurrentThread.ManagedThreadId.ToString() + "]", ex); item = new ListViewItem(); item.Tag = "[local]OpenError"; item.Text = "[local]OpenError"; item.ToolTipText = ex.ToString(); item.SubItems.Add(TID); item.SubItems.Add(DateTime.Now.ToString()); AddToDisplay(lstEvents, item); //System.Diagnostics.Debug.WriteLine("-"); //System.Diagnostics.Debug.WriteLine("Error Opening StreamingSubscriptionConnection [TID:" + Thread.CurrentThread.ManagedThreadId.ToString() + "]", ex); //System.Diagnostics.Debug.WriteLine("-"); } try { // Close Connection if (CurrentConnection.IsOpen) { CurrentConnection.Close(); // Thread.Sleep(500); } } catch (Exception ex) { DebugLog.WriteException("Error Closing Streaming Connection [TID:" + Thread.CurrentThread.ManagedThreadId.ToString() + "]", ex); item = new ListViewItem(); item.Tag = "[local]CloseError"; item.Text = "[local]CloseError"; item.ToolTipText = ex.ToString(); item.SubItems.Add(TID); item.SubItems.Add(DateTime.Now.ToString()); AddToDisplay(lstEvents, item); //System.Diagnostics.Debug.WriteLine("-"); //System.Diagnostics.Debug.WriteLine("Error Closing Streaming Connection [TID:" + Thread.CurrentThread.ManagedThreadId.ToString() + "]", ex); //System.Diagnostics.Debug.WriteLine("-"); } finally { lock (ActiveConnections) { ActiveConnections.Remove(CurrentConnection); SetControlText(ConnectionCount, ActiveConnections.Count.ToString()); } } // Remove Handlers CurrentConnection.OnDisconnect -= OnDisconnect; CurrentConnection.OnSubscriptionError -= OnSubscriptionError; CurrentConnection.OnNotificationEvent -= OnStreamingEvent; foreach (StreamingSubscription CurrentSubscription in ThreadLocalSubscriptions) { try { CurrentConnection.RemoveSubscription(CurrentSubscription); CurrentSubscription.Unsubscribe(); //System.Diagnostics.Debug.WriteLine("-"); //System.Diagnostics.Debug.WriteLine("Unsubscribed - ID: " + CurrentSubscription.Id + " Watermark: " + CurrentSubscription.Watermark); //System.Diagnostics.Debug.WriteLine("-"); } catch (Exception ex) { DebugLog.WriteException("Error Removing/Unsubscribing StreamingSubscription Elements [TID:" + Thread.CurrentThread.ManagedThreadId.ToString() + "]", ex); item = new ListViewItem(); item.Tag = "[local]UnsubscribeError"; item.Text = "[local]UnsubscribeError"; item.ToolTipText = ex.ToString(); item.SubItems.Add(TID); item.SubItems.Add(DateTime.Now.ToString()); AddToDisplay(lstEvents, item); //System.Diagnostics.Debug.WriteLine("-"); //System.Diagnostics.Debug.WriteLine("Error Removing/Unsubscribing StreamingSubscription Elements [TID:" + Thread.CurrentThread.ManagedThreadId.ToString() + "]", ex); //System.Diagnostics.Debug.WriteLine(" ID: " + CurrentSubscription.Id + " Watermark: " + CurrentSubscription.Watermark); //System.Diagnostics.Debug.WriteLine("-"); } finally { lock (ActiveSubscriptions) { //System.Diagnostics.Debug.WriteLine("-"); //System.Diagnostics.Debug.WriteLine("Removing subscription - ID: " + CurrentSubscription.Id + " Watermark: " + CurrentSubscription.Watermark); //System.Diagnostics.Debug.WriteLine("-"); ActiveSubscriptions.Remove(CurrentSubscription); SetControlText(SubscriptionCount, ActiveSubscriptions.Count.ToString()); } } } lock (WorkThreads) { WorkThreads.Remove(Thread.CurrentThread); } SetControlText(ThreadCount, WorkThreads.Count.ToString()); } catch (Exception ex) { DebugLog.WriteException("Unexpected Exception in WorkerThread", ex); } finally { } }