Esempio n. 1
0
        /// <summary>
        /// Searches the Configuration Providers for an entry with the specified name
        /// </summary>
        /// <param name="name">Name of the entry to find</param>
        /// <returns>Value of the found entry or null if none found</returns>
        public static object GetSetting(string name)
        {
            object returnValue = null;

            _lock.AcquireReaderLock(LockTimeout);

            if (_lock.IsReaderLockHeld)
            {
                try
                {
                    if (_settings != null)
                    {
                        returnValue = _settings.GetSetting(name);
                    }
                    else
                    {
                        LockCookie lockCookie = _lock.UpgradeToWriterLock(LockTimeout);
                        if (_lock.IsWriterLockHeld)
                        {
                            _settings = new InternalSettings();
                            _lock.DowngradeFromWriterLock(ref lockCookie);
                            returnValue = _settings.GetSetting(name);
                        }
                        else
                        {
                            throw new ApplicationException("Could not obtain lock to reopen settings system");
                        }
                    }
                }
                finally
                {
                    _lock.ReleaseReaderLock();
                }
            }
            return returnValue;
        }
Esempio n. 2
0
        /// <summary>
        /// Closes the settings system calling dispose on any SettingsProviders that implement IDisposable.  
        /// If GetSetting is called after Close has been called then the settings system will be reopened
        /// </summary>
        public static void Close()
        {
            _lock.AcquireWriterLock(LockTimeout);

            try
            {
                if (_lock.IsWriterLockHeld)
                {
                    if (_settings != null)
                    {
                        _settings.Dispose();
                        _settings = null;
                    }
                }
                else
                {
                    throw new ApplicationException("Could not obtain lock to close settings system");
                }
            }
            finally 
            {
                if (_lock.IsWriterLockHeld)
                {
                    _lock.ReleaseWriterLock();
                }
            }
        }