コード例 #1
0
        /// <summary>
        /// Removes the <see cref="StreamingSubscription"/> and starts listening again only if any other subscriptions are present.
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Remove(string userMailAddress)
        {
            bool success;
            var  mailAddress = new MailAddress(userMailAddress);

            if (this._subscriptions.ContainsKey(mailAddress) == false)
            {
                return(false);
            }

            lock (_conLock)
            {
                this.isClosingControlled = true;
                var subscriptionToRemove = _subscriptions[mailAddress];

                if (_connection.IsOpen)
                {
                    _connection.Close();
                }

                _connection.RemoveSubscription(subscriptionToRemove);
                success = _subscriptions.Remove(mailAddress);
                if (this._subscriptions.Any())
                {
                    _connection.Open();
                }

                this.isClosingControlled = false;
            }
            return(success);
        }
コード例 #2
0
    private void CreateSubscription()
    {
        var ids = new FolderId[2] {
            new FolderId(WellKnownFolderName.Root), new FolderId(WellKnownFolderName.Inbox)
        };
        var events = new List <EventType>();

        events.Add(EventType.NewMail);
        if (_subscription != null)
        {
            ((StreamingSubscription)_subscription).Unsubscribe();
            _connection.RemoveSubscription((StreamingSubscription)_subscription);
        }
        _subscription = _exchange.SubscribeToStreamingNotifications(ids, events.ToArray());
        _connection.AddSubscription((StreamingSubscription)_subscription);
    }
コード例 #3
0
ファイル: RfiCoder.cs プロジェクト: kg4mfq/RfiCoder
        /// <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);
        }
コード例 #4
0
        ////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 { }
        }