Exemplo n.º 1
0
 static internal InvalidOperationException UnexpectedError(string methodName)
 {
     return(new InvalidOperationException(SR.GetString(SR.Unexpected_Error, methodName)));
 }
Exemplo n.º 2
0
 static internal ArgumentException PropertyInvalid(string property)
 {
     return(new ArgumentException(SR.GetString(SR.Property_Invalid, property), property));
 }
Exemplo n.º 3
0
 static internal ArgumentException PropertyNullOrEmpty(string property)
 {
     return(new ArgumentException(SR.GetString(SR.Property_NullOrEmpty, property), property));
 }
Exemplo n.º 4
0
 static internal ArgumentException ParameterNullOrEmpty(string parameter)
 {
     return(new ArgumentException(SR.GetString(SR.Parameter_NullOrEmpty, parameter), parameter));
 }
Exemplo n.º 5
0
 static internal ArgumentException ParameterInvalid(string parameter)
 {
     return(new ArgumentException(SR.GetString(SR.Parameter_Invalid, parameter), parameter));
 }
        static internal ConfigurationErrorsException WrapAsConfigException(string outerMessage, Exception e, string filename, int line)
        {
            //
            // Preserve ConfigurationErrorsException
            //
            ConfigurationErrorsException ce = e as ConfigurationErrorsException;

            if (ce != null)
            {
                // Well... mostly preserve. Add file/line info if it's missing.
                if ((filename != null) && (ce.Filename == null))
                {
                    ce.SetFileAndLine(filename, line);
                }
                return(ce);
            }

            //
            // Promote deprecated ConfigurationException to ConfigurationErrorsException
            //
            ConfigurationException deprecatedException = e as ConfigurationException;

            if (deprecatedException != null)
            {
                return(new ConfigurationErrorsException(deprecatedException));
            }

            //
            // For XML exceptions, preserve the text of the exception in the outer message.
            //
            XmlException xe = e as XmlException;

            if (xe != null)
            {
                if (xe.LineNumber != 0)
                {
                    line = xe.LineNumber;
                }

                return(new ConfigurationErrorsException(xe.Message, xe, filename, line));
            }

            //
            // Wrap other exceptions in an inner exception, and give as much info as possible
            //
            if (e != null)
            {
                return(new ConfigurationErrorsException(
                           SR.GetString(SR.Wrapped_exception_message, outerMessage, e.Message),
                           e,
                           filename,
                           line));
            }

            //
            // If there is no exception, create a new exception with no further information.
            //
            return(new ConfigurationErrorsException(
                       SR.GetString(SR.Wrapped_exception_message, outerMessage, ExceptionUtil.NoExceptionInformation),
                       filename,
                       line));
        }
Exemplo n.º 7
0
        // Ensure that initialization has completed, while handling re-entrancy issues
        // for certain sections that may be used during initialization itself.
        void EnsureInit(string configKey)
        {
            bool doInit = false;

            lock (this) {
                // If the user config is not initialized, then we must either:
                //    a. Perform the initialization ourselves if no other thread is doing it, or
                //    b. Wait for the initialization to complete if we know the section is not used during initialization itself, or
                //    c. Ignore initialization if the section can be used during initialization. Note that GetSection()
                //       returns null is initialization has not completed.
                if (!_isUserConfigInited)
                {
                    if (!_isInitInProgress)
                    {
                        _isInitInProgress = true;
                        doInit            = true;
                    }
                    else if (!IsSectionUsedInInit(configKey))
                    {
                        // Wait for initialization to complete.
                        Monitor.Wait(this);
                    }
                }
            }

            if (doInit)
            {
                try {
                    try {
                        try {
                            // Initialize machine configuration.
                            _machineConfigRecord = _configRoot.GetConfigRecord(
                                ClientConfigurationHost.MachineConfigPath);

                            _machineConfigRecord.ThrowIfInitErrors();

                            // Make machine configuration available to system.net sections
                            // when application configuration is downloaded via http.
                            _isMachineConfigInited = true;

                            //
                            // Prevent deadlocks in the networking classes by loading
                            // networking config before making a networking request.
                            // Any requests for sections used in initialization during
                            // the call to EnsureConfigLoaded() will be served by
                            // _machine.config or will return null.
                            //
                            if (_isAppConfigHttp)
                            {
                                ConfigurationManagerHelperFactory.Instance.EnsureNetConfigLoaded();
                            }

                            //
                            // Now load the rest of configuration
                            //
                            _configHost.RefreshConfigPaths();
                            string configPath;
                            if (_configHost.HasLocalConfig)
                            {
                                configPath = ClientConfigurationHost.LocalUserConfigPath;
                            }
                            else if (_configHost.HasRoamingConfig)
                            {
                                configPath = ClientConfigurationHost.RoamingUserConfigPath;
                            }
                            else
                            {
                                configPath = ClientConfigurationHost.ExeConfigPath;
                            }

                            _completeConfigRecord = _configRoot.GetConfigRecord(configPath);
                            _completeConfigRecord.ThrowIfInitErrors();

                            _isUserConfigInited = true;
                        }
                        catch (Exception e) {
                            _initError = new ConfigurationErrorsException(SR.GetString(SR.Config_client_config_init_error), e);
                            throw _initError;
                        }
                        catch {
                            _initError = new ConfigurationErrorsException(SR.GetString(SR.Config_client_config_init_error));
                            throw _initError;
                        }
                    }
                    catch {
                        ConfigurationManager.SetInitError(_initError);
                        _isMachineConfigInited = true;
                        _isUserConfigInited    = true;
                        throw;
                    }
                }
                finally {
                    lock (this) {
                        try {
                            // Notify ConfigurationSettings that initialization has fully completed,
                            // even if unsuccessful.
                            ConfigurationManager.CompleteConfigInit();

                            _isInitInProgress = false;
                        }
                        finally {
                            // Wake up all threads waiting for initialization to complete.
                            Monitor.PulseAll(this);
                        }
                    }
                }
            }
        }
        internal ConfigurationProperty(PropertyInfo info)
        {
            Debug.Assert(info != null, "info != null");

            // Bellow are the attributes we handle
            TypeConverterAttribute          attribConverter = null;
            ConfigurationPropertyAttribute  attribProperty  = null;
            ConfigurationValidatorAttribute attribValidator = null;

            // Compatability attributes
            // If the approprite data is provided in the ConfigPropAttribute then the one bellow will be ignored
            DescriptionAttribute  attribStdDescription = null;
            DefaultValueAttribute attribStdDefault     = null;

            TypeConverter typeConverter          = null;
            ConfigurationValidatorBase validator = null;

            // Find the interesting attributes in the collection
            foreach (Attribute attribute in Attribute.GetCustomAttributes(info))
            {
                if (attribute is TypeConverterAttribute)
                {
                    attribConverter = (TypeConverterAttribute)attribute;
                    typeConverter   = (TypeConverter)TypeUtil.CreateInstanceWithReflectionPermission(attribConverter.ConverterTypeName);
                }
                else if (attribute is ConfigurationPropertyAttribute)
                {
                    attribProperty = (ConfigurationPropertyAttribute)attribute;
                }
                else if (attribute is ConfigurationValidatorAttribute)
                {
                    if (validator != null)
                    {
                        throw new ConfigurationErrorsException(SR.GetString(SR.Validator_multiple_validator_attributes, info.Name));
                    }

                    attribValidator = (ConfigurationValidatorAttribute)attribute;
                    validator       = attribValidator.ValidatorInstance;
                }
                else if (attribute is DescriptionAttribute)
                {
                    attribStdDescription = (DescriptionAttribute)attribute;
                }
                else if (attribute is DefaultValueAttribute)
                {
                    attribStdDefault = (DefaultValueAttribute)attribute;
                }
            }

            Type propertyType = info.PropertyType;

            // Collections need some customization when the collection attribute is present
            if (typeof(ConfigurationElementCollection).IsAssignableFrom(propertyType))
            {
                ConfigurationCollectionAttribute attribCollection =
                    Attribute.GetCustomAttribute(info,
                                                 typeof(ConfigurationCollectionAttribute)) as ConfigurationCollectionAttribute;

                // If none on the property - see if there is an attribute on the collection type itself
                if (attribCollection == null)
                {
                    attribCollection =
                        Attribute.GetCustomAttribute(propertyType,
                                                     typeof(ConfigurationCollectionAttribute)) as ConfigurationCollectionAttribute;
                }
                if (attribCollection != null)
                {
                    if (attribCollection.AddItemName.IndexOf(',') == -1)
                    {
                        _addElementName = attribCollection.AddItemName;
                    }
                    _removeElementName = attribCollection.RemoveItemName;
                    _clearElementName  = attribCollection.ClearItemsName;
                }
            }

            // This constructor shouldnt be invoked if the reflection info is not for an actual config property
            Debug.Assert(attribProperty != null, "attribProperty != null");

            ConstructorInit(attribProperty.Name,
                            info.PropertyType,
                            attribProperty.Options,
                            validator,
                            typeConverter);

            // Figure out the default value
            InitDefaultValueFromTypeInfo(attribProperty, attribStdDefault);

            // Get the description
            if ((attribStdDescription != null) && !string.IsNullOrEmpty(attribStdDescription.Description))
            {
                _description = attribStdDescription.Description;
            }
        }