private bool HandlePropertySetException(SettingsBase settings, SettingsStorageModel property, Exception ex, bool?throwSetException = null)
        {
            throwSetException = throwSetException ?? ThrowPropertySetException;

            if (!throwSetException.Value)
            {
                string caughtExceptionDetails = string.Format("{0}-{1}", ex.GetType().Name, property.Name);

                _periodicReaderErrors.Add(caughtExceptionDetails);

                if (PropertyError != null) //only raise event when not throttling
                {
                    var propertyException = new SettingsPropertyException(
                        String.Format("Error setting property {0}.{1}", settings.Category, property.Name),
                        property.Name,
                        settings.Category,
                        ex);

                    var repeatingErrorEventArgs = new RepeatingErrorEventArgs()
                    {
                        Exception   = propertyException,
                        IsRepeating = _periodicReaderErrors.Contains(caughtExceptionDetails)
                    };

                    PropertyError.Invoke(settings, repeatingErrorEventArgs);
                }
                return(true);
            }
            return(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Looks for changes in underlying repository and updates all SettingsBase (registered with this SettingsManager) objects with changed values
        /// Respects all error handling settings;
        /// </summary>
        public void UpdateChangedSettings()
        {
            try
            {
                lock (SyncRoot)
                {
                    if (ReaderExecuting != null)
                    {
                        ReaderExecuting.Invoke(this, new EventArgs());
                    }
                    var now = Now();
                    var settingsFromRepo = Repository.ReadSettings(GetCategoriesToRead(), _lastRead).ToNonNullArray();
                    if (settingsFromRepo.Length == 0)
                    {
                        return;
                    }
                    foreach (SettingsBase settings in AllSettings.Values)
                    {
                        var category         = settings.Category;
                        var matchingSettings = settingsFromRepo.Where(s => s.Category == category);
                        SetProperties(settings, matchingSettings, ThrowPropertySetExceptionsOnPeriodicRead);
                    }
                    foreach (var f in settingsFromRepo.Where(s => s.Category == FlagsCategoryName))
                    {
                        if (SetFlag(f.Name, f.Value))
                        {
                            if (FlagChanged != null)
                            {
                                FlagChanged.Invoke(this, new PropertyChangedEventArgs(f.Name));
                            }
                        }
                    }

                    _lastRead = settingsFromRepo.Max(s => s.UpdatedAt);
                    //sets last read time to newest found setting
                    if (_lastRead > now) //last read time must not be greated than current time
                    {
                        _lastRead = now;
                    }
                }
            }
            catch (Exception ex)
            {
                _periodicReaderErrors.FlushOld(Now());
                if (PeriodicReaderError != null)
                {
                    var args = new RepeatingErrorEventArgs()
                    {
                        Exception   = ex,
                        IsRepeating = _periodicReaderErrors.Contains(ex.GetType().Name)
                    };
                    PeriodicReaderError.Invoke(this, args);
                }
                _periodicReaderErrors.Add(ex.GetType().Name);
            }
        }
        /// <summary>
        /// Looks for changes in underlying repository and updates all SettingsBase (registered with this SettingsManager) objects with changed values
        /// Respects all error handling settings; 
        /// </summary>
        public void UpdateChangedSettings()
        {
            try
            {
                lock (SyncRoot)
                {

                    if (ReaderExecuting != null)
                        ReaderExecuting.Invoke(this, new EventArgs());
                    var now = Now();
                    var settingsFromRepo = Repository.ReadSettings(GetCategoriesToRead(), _lastRead).ToNonNullArray();
                    if (settingsFromRepo.Length == 0)
                        return;
                    foreach (SettingsBase settings in AllSettings.Values)
                    {
                        var category = settings.Category;
                        var matchingSettings = settingsFromRepo.Where(s => s.Category == category);
                        SetProperties(settings, matchingSettings, ThrowPropertySetExceptionsOnPeriodicRead);
                    }
                    foreach (var f in settingsFromRepo.Where(s => s.Category == FlagsCategoryName))
                    {
                        if (SetFlag(f.Name, f.Value))
                        {
                            if (FlagChanged != null)
                                FlagChanged.Invoke(this, new PropertyChangedEventArgs(f.Name));
                        }
                    }

                    _lastRead = settingsFromRepo.Max(s => s.UpdatedAt);
                    //sets last read time to newest found setting
                    if (_lastRead > now) //last read time must not be greated than current time
                        _lastRead = now;
                }
            }
            catch (Exception ex)
            {
                _periodicReaderErrors.FlushOld(Now());
                if (PeriodicReaderError != null)
                {
                    var args = new RepeatingErrorEventArgs()
                    {
                        Exception = ex,
                        IsRepeating = _periodicReaderErrors.Contains(ex.GetType().Name)
                    };
                    PeriodicReaderError.Invoke(this, args);
                }
                _periodicReaderErrors.Add(ex.GetType().Name);
            }
        }