Exemplo n.º 1
0
        internal void Validate(bool loading)
        {
            List <string> missing = null;

            foreach (ConfigurationAttributeSchema attribute in Schema.AttributeSchemas)
            {
                if (!attribute.IsRequired)
                {
                    continue;
                }

                if (!RawAttributes.ContainsKey(attribute.Name))
                {
                    if (missing == null)
                    {
                        missing = new List <string>();
                    }

                    missing.Add(attribute.Name);
                }
            }

            if (missing != null)
            {
                string error;
                if (!loading)
                {
                    error = $"required attributes {StringExtensions.Combine(missing, ",")}";
                }
                else if (missing.Count == 1)
                {
                    error = $"required attribute '{missing[0]}'";
                }
                else
                {
                    error = $"required attributes '{StringExtensions.Combine(missing, ",")}'";
                }

                if (loading)
                {
                    var line = (Entity as IXmlLineInfo).LineNumber;
                    throw new COMException(
                              string.Format("Line number: {0}\r\nError: Missing {1}\r\n", line, error));
                }

                throw new FileNotFoundException($"Filename: \r\nError: Element is missing {error}\r\n\r\n");
            }
        }
Exemplo n.º 2
0
        internal object SetAttributeValueInner(string name, object value)
        {
            if (Section.IsLocked)
            {
                throw new FileLoadException("This configuration section cannot be used at this path. This happens when the section is locked at a parent level. Locking is either by default (overrideModeDefault=\"Deny\"), or set explicitly by a location tag with overrideMode=\"Deny\" or the legacy allowOverride=\"false\".\r\n");
            }

            if (!this.SkipCheck)
            {
                this.FileContext.SetDirty();
            }

            var attribute = this.GetAttribute(name);
            var result    = attribute.TypeMatch(value);

            attribute.IsInheritedFromDefaultValue = (attribute.Schema == null || !attribute.Schema.IsRequired) &&
                                                    result.Equals(attribute.ExtractDefaultValue());
            // IMPORTANT: remove attribute if value is equal to default.
            this.Entity.SetAttributeValue(
                name, attribute.IsInheritedFromDefaultValue
                    ? null
                    : attribute.Format(result));

            // IMPORTANT: IIS seems to use the following
            // this.Entity.SetAttributeValue(name, attribute.Format(_value));

            this.CleanEntity();
            if (attribute.IsInheritedFromDefaultValue)
            {
                this.RawAttributes.Remove(name);
            }
            else
            {
                if (RawAttributes.ContainsKey(name))
                {
                    RawAttributes[name] = value.ToString();
                }
                else
                {
                    RawAttributes.Add(name, value.ToString());
                }
            }

            return(result);
        }