Ejemplo n.º 1
0
        /// <summary>
        ///     Use this to get notified of events, this uses StreamingNotifications
        /// </summary>
        /// <param name="wellKnownFolderName">WellKnownFolderName to look to, Inbox if none is specified</param>
        /// <param name="eventTypes">params EventType, if nothing specified than EventType.NewMail is taken</param>
        /// <returns>IObservable which publishes NotificationEvent</returns>
        public IObservable <NotificationEvent> Observe(WellKnownFolderName wellKnownFolderName = WellKnownFolderName.Inbox, params EventType[] eventTypes)
        {
            if (eventTypes == null || eventTypes.Length == 0)
            {
                eventTypes = new[] { EventType.NewMail };
            }

            return(Observable.Create <NotificationEvent>(
                       observer =>
            {
                try
                {
                    var streamingSubscription = Service.SubscribeToStreamingNotifications(new FolderId[] { wellKnownFolderName }, eventTypes);

                    var connection = new StreamingSubscriptionConnection(Service, 1);
                    connection.AddSubscription(streamingSubscription);
                    connection.OnNotificationEvent += (sender, notificationEventArgs) =>
                    {
                        foreach (var notificationEvent in notificationEventArgs.Events)
                        {
                            observer.OnNext(notificationEvent);
                        }
                    };
                    // Handle errors
                    connection.OnSubscriptionError += (sender, subscriptionErrorEventArgs) => observer.OnError(subscriptionErrorEventArgs.Exception);

                    // Use this to
                    bool disposedConnection = false;

                    // As the time to live is maximum 30 minutes, the connection is closed by the server
                    connection.OnDisconnect += (sender, subscriptionErrorEventArgs) =>
                    {
                        if (subscriptionErrorEventArgs.Exception != null || disposedConnection)
                        {
                            return;
                        }
                        // Connection closed by server, just reconnect
                        // See: https://msdn.microsoft.com/en-us/library/office/hh312849.aspx
                        // "This behavior is expected and is not an error condition"
                        connection.Open();
                    };

                    // Start the connection
                    connection.Open();

                    // Return a disposable which disposed the connection and unsubscribes the subscription
                    return Disposable.Create(() =>
                    {
                        disposedConnection = true;
                        connection.Dispose();
                        streamingSubscription.Unsubscribe();
                    });
                }
                catch (Win32Exception e)
                {
                    observer.OnError(e);
                    return Disposable.Empty;
                }
            }));
        }
Ejemplo n.º 2
0
 public void Dispose()
 {
     lock (_conLock)
     {
         isClosingControlled = true;
         if (_connection.IsOpen)
         {
             _connection.Close();
         }
         _connection.Dispose();
         isClosingControlled = false;
     }
 }
Ejemplo n.º 3
0
        public void Disconnect()
        {
            if (subconn != null)
            {
                try
                {
                    if (subconn.IsOpen)
                    {
                        subconn.Close();
                    }

                    subconn.Dispose();
                }
                catch { }
                finally
                {
                    subconn = null;
                }
            }

            if (exitToken != null)
            {
                try
                {
                    exitToken.Cancel(false);
                }
                catch { }
            }

            if (mainThread != null && !mainThread.Join(3000))
            {
                try
                {
                    Debugger.Break();
                    mainThread.Abort();
                }
                catch { }
            }

            mainThread = null;

            try
            {
                exitToken.Dispose();
            }
            catch { }
            finally
            {
                exitToken = new CancellationTokenSource();
            }
        }
Ejemplo n.º 4
0
        private void Connection_OnSubscriptionError(object sender, SubscriptionErrorEventArgs args)
        {
            Debugger.Break();

            StreamingSubscriptionConnection connection = sender as StreamingSubscriptionConnection;

            if (!connection.IsOpen)
            {
                connection.Close();
            }

            connection.Dispose();
            Status = EProviderStatus.Error;
            Connect();
        }
Ejemplo n.º 5
0
        private void Connection_OnDisconnect(object sender, SubscriptionErrorEventArgs args)
        {
            StreamingSubscriptionConnection connection = sender as StreamingSubscriptionConnection;

            try
            {
                connection.Open();
            }
            catch (Exception ex)
            {
                if (Status != EProviderStatus.Error)
                {
                    Status = EProviderStatus.Offline;
                }

                connection.Dispose();
                Connect();
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Stop this service.
        /// </summary>
        protected override void OnStop()
        {
            try {
                emailSubscriptionConnection.Close();
            } catch (Microsoft.Exchange.WebServices.Data.ServiceLocalException localException) {
            }

            emailSubscriptionConnection.RemoveSubscription(emailSubscription);

            emailSubscriptionConnection.Dispose();

            Data.RfiIndexTable.Index.Dispose();

            Data.SpamIndexTable.Index.Dispose();

            //impersonator.Dispose();

            this.eventLog.WriteEntry("RfiCoder has stopped", EventLogEntryType.Information);
            Logger.LoggerAsync.InstanceOf.GeneralLogger.Info("RfiCoder has stopped: {0}", EventLogEntryType.Information);
        }
 /// <summary>
 /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
 /// </summary>
 public void Dispose()
 {
     Stop();
     _subscriptionConnection.Dispose();
 }