Exemplo n.º 1
0
        private bool ParseSections(XElement element, ConfigurationElement parent, Location location)
        {
            var result = false;
            var path   = element.GetPath();
            ConfigurationElement node = null;
            var schema = FindSchema(path);
            var sec    = FindSectionSchema(path);
            var name   = element.Name.LocalName;

            if (schema != null)
            {
                if (sec != null)
                {
                    var section = new ConfigurationSection(path, sec.Root, location?.Path, this, element);
                    RootSectionGroup.Add(section, location);
                    node = section;
                }
                else
                {
                    node = schema.CollectionSchema == null
                               ? new ConfigurationElement(null, name, schema, parent, element, this)
                               : new ConfigurationElementCollection(
                        name,
                        schema,
                        parent,
                        element,
                        this);
                }

                parent?.AddChild(node);
                result = true;
            }
            else
            {
                var found = RootSectionGroup.GetSectionDefinition(path);
                if (found != null && found.Ignore && path != "system.webServer")
                {
                    return(true);
                }
            }

            foreach (var item in element.Nodes())
            {
                var child = item as XElement;
                if (child == null)
                {
                    continue;
                }

                var childAdded = ParseSections(child, node, location);
                result = result || childAdded;
            }

            if (!result && !_dontThrow && name != "location")
            {
                throw new COMException(
                          string.Format(
                              "Line number: {0}\r\nError: Unrecognized element '{1}'\r\n",
                              (element as IXmlLineInfo).LineNumber,
                              name));
            }

            return(result);
        }
Exemplo n.º 2
0
        private bool ParseSections(XElement element, ConfigurationElement parent, Location location)
        {
            var result = false;
            var path   = element.GetPath();
            ConfigurationElement node = null;
            var schema = FindSchema(path);
            var sec    = FindSectionSchema(path);
            var name   = element.Name.LocalName;

            if (schema != null)
            {
                if (sec != null)
                {
                    var section = new ConfigurationSection(path, sec.Root, location?.Path, this, element);
                    RootSectionGroup.Add(section, location);
                    node = section;
                }
                else
                {
                    node = schema.CollectionSchema == null
                               ? new ConfigurationElement(null, name, schema, parent, element, this)
                               : new ConfigurationElementCollection(
                        name,
                        schema,
                        parent,
                        element,
                        this);
                }

                parent?.AddChild(node);
                result = true;
            }
            else
            {
                var found = RootSectionGroup.GetSectionDefinition(path);
                if (found != null && found.Ignore)
                {
                    if (path == "runtime")
                    {
                        // examples: <runtime> in machine.config.
                        return(true);
                    }
                    else
                    {
                        if (!element.HasElements)
                        {
                            // like empty tag in web.config.
                            return(true);
                        }
                    }
                }
                else
                {
                    // TODO: improve performance.
                    var foundChild = RootSectionGroup.GetChildSectionDefinition(path);
                    if (foundChild != null && !element.HasElements)
                    {
                        return(true);
                    }
                }
            }

            foreach (var item in element.Nodes())
            {
                var child = item as XElement;
                if (child == null)
                {
                    continue;
                }

                var childAdded = ParseSections(child, node, location);
                result = result || childAdded;
            }

            if (!result && !_dontThrow && name != "location")
            {
                string link = null;
                string oob  = null;
                if (path.StartsWith("system.webServer/aspNetCore/"))
                {
                    oob  = "ASP.NET Core Module (system.webServer/aspNetCore/)";
                    link = "https://docs.microsoft.com/en-us/aspnet/core/publishing/iis?tabs=aspnetcore2x#install-the-net-core-windows-server-hosting-bundle";
                }
                else if (path.StartsWith("system.webServer/rewrite/"))
                {
                    oob  = "URL Rewrite Module (system.webServer/rewrite/)";
                    link = "https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/using-the-url-rewrite-module#where-to-get-the-url-rewrite-module";
                }
                else if (path.StartsWith("system.webServer/webFarms/"))
                {
                    oob  = "Application Request Routing Module (system.webServer/webFarms/)";
                    link = "https://docs.microsoft.com/en-us/iis/extensions/configuring-application-request-routing-arr/define-and-configure-an-application-request-routing-server-farm#prerequisites";
                }

                var exception = new COMException(
                    string.Format(
                        "Line number: {0}\r\nError: Unrecognized element '{1}'\r\n",
                        (element as IXmlLineInfo).LineNumber,
                        name));
                if (oob != null)
                {
                    exception.Data.Add("oob", oob);
                    exception.Data.Add("link", link);
                }

                throw exception;
            }

            return(result);
        }