GetSection("system.webServer/myCustomSectionName") not working :(
Someone please help me with this:
I am trying to read custom web.config section stored under system.webServer section...
something that looks like that:<system.webServer>
<myCustomSectionName>
<myFirstElement>
<myinfo value="123"></myinfo>
<myfolder path="xxx"></myfolder>
</myFirstElement>
</myCustomSectionName>
</system.webServer>Schema .xml and applicationHost.config are updated correctly, IIS can load it.
This is how I read the web.config file (it works)
Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/");
//This line works and returns my custom config element within system.webServer (as XML)!
1. string rawXML = config.GetSection("system.webServer").SectionInformation.GetRawXml();//This is not working
2. string rawXMLMyCustomSection = config.GetSection("system.webServer/myCustomSectionName").SectionInformation.GetRawXml();More:
1. Response.Write(config.GetSection("system.webServer") == null)) - Gives "FALSE" (there is something)
2. Response.Write(config.GetSection("system.webServer/myCustomSectionName") == null)) - Gives "TRUE" (null) -- This is very strange, I would say :)Please help me with it as I can't find the source of the problem here...
It might be something todo with Runtime limitation on reading configuration sections or reflection or something that was not set correctly somewhere?
Is it at all possible to do what I am trying? I guess it should be, but I couldn't find any documentation on that.I know that I could use "Microsoft.Web.Administration" namespace and the "ServerManager" class, but I would not like to add an additional dependency on this .DLL
within my project - it should be as lightweighted as possible.Thank you guys in advance!
All Replies
- Hello
I used .NET Reflector to study the implementation of Configuration.GetSection.
The function calls
record = (SectionRecord) this._sectionRecords[configKey]; of BaseConfigurationRecord
to get the SectionRecord element.
_sectionRecords is a hashtable, and I see that the field does not have "system.webServer/myCustomSectionName" value. I'm digging more into it. I will update you soon.
Regards,
Jialiang Ge
MSDN Subscriber Support in Forum
If you have any feedback of our support, please contact msdnmg@microsoft.com.
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us. - Thank you!
It seems like you are using the IIS configuraiton system, which means you are already schematizing the IIS configuration using an XML file in system32\inetsrv\config\schema. If that is the case the best way of reading it is using Microsoft.Web.Administration instead of System.Configuration.
The biggest reason is that System.Configuration has no knowledge of ApplicationHost.config or the way our schema works. This means that they will not do any inheritance of configuration that you set in ApplicationHost.config.
So here is my recommendation:
1) If you want to support ApplicaitonHost.config and use IIS 7, then I would change your code to do:
Microsoft.Web.Administration.Configuration config = Microsoft.Web.Administration.WebConfigurationManager.GetSection("system.webServer/myCustomSectionName");//This line works and returns my custom config element within system.webServer (as XML)!
a. string rawXML = config.GetSection("system.webServer").SectionInformation.GetRawXml();//This is not working
b. value = config.GetElementByName("SomeElement")["SomeProperty"]
2) If you do not care necesarilly about the beauty of ApplicationHost.config (and things like Shared Configuration, centralized management, delegation, etc), then you should use System.Configuration and its extensibility model. This means that you need to write a class named Section Handler that tells .net how to parse configuration. In general .net configuration system is typed through code and really using loosely type code is not a good option, they really do not do a good job parsing unless they have a class to handle the parsing.- Thank you for your relpy!
Thing is that my intention is not to read a configuration section from ApplicationHost.config, but from a web.config within a site.
For example, the actual configuration would be 99% similar to one used by the URL Rewriting module. (<rewrite><rules><rule .....)
I need to read this configuration from a managed HTTP Module - on Init - so the module would know the rules it should apply to incoming
request URL's. The module would probably sit in the GAC and is intended to be shipped as a product
(so, for multiple computers with pretty much unknown environments)
I managed to read my config and pass it to a custom settings class where XML is mapped to class members, but as
System.Configuration...GetSection("system.webServer")...XmlDocument.LoadXml(RawXML...)
I just had to know why using System.Configuration..GetSection("system.webServer/MyCustomElement") would not work.
Is there a place where more documentation can be found on this topic? Besides the basic "Extending IIS configuration" articles ?
Thank you very much again! :)
- I dont know of specific places to read more about the information other than http://www.iis.net/ however I can tell you why that does not work.
Basically if you open machine.config you will see that system.webServer is added as a section, and not only that but is specifically mapped to the IgnoreSectionHandler so that system.configuration ignores any content within it.
<section name="system.webServer" type="System.Configuration.IgnoreSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
I would probably recommend to move your section away from system.webServer and write your own section handler that using System.Configuration derived classes gives you the parsed version instead of just the XML. Basically the idea is that you would register in your web.config or in machine.config your section handler something like:
<section name="system.webServer/yourCustomSection" type="YourOwnAssembly.YourNamespace.YourConfigurationSectionDerivedClass, YourOwnAssembly, Version=.."/>
Is there any reason why you do not want to use the normal System.Configuration parsing mechanisms and just get the raw XML? - Thanks, Carlos, for the suggestion!
Hello Denis, may I know the current status of the issue on your side?
Regards,
Jialiang Ge
MSDN Subscriber Support in Forum
If you have any feedback of our support, please contact msdnmg@microsoft.com.
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.


