none
GetSection("system.webServer/myCustomSectionName") not working :( RRS feed

  • Question

  • 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!

    Monday, November 2, 2009 3:10 PM

Answers

  • 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?


    Saturday, November 7, 2009 5:43 AM

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.
    Tuesday, November 3, 2009 9:43 AM
    Moderator
  • Thank you!
    Tuesday, November 3, 2009 9:47 AM
  • 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.





    Tuesday, November 3, 2009 9:59 PM
  • 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! :)
     
    Wednesday, November 4, 2009 9:47 AM
  • 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?


    Saturday, November 7, 2009 5:43 AM
  • 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.
    Tuesday, November 17, 2009 3:22 PM
    Moderator
  • I apologize for my delayed reply. So I ended up writing my own configuration sections parsing class, that would take the raw XML as input (with a stipped off namespace declaration). The reason for that is I only needed to read a sequence of "rule" elements, along with a few custom elements - with some lightweight method that does not require any special components like Microsoft.Web.Administration.Configuration. Once I would read the configuration from an HTTP module and once the same configuration would be read by a Windows Service application - same method nicely applies for both. Also, writing a custom configuration parsing class gave me more flexibility over a few things I required. Thank you very much for your help.

    I see what CarlosAg is saying above - and am thankful for his clear answer. I just wanted my configuration section to resemble the one of "IIS URL Rewrite" module and have it positioned somewhere next to it, within system.webServer section.
    Wednesday, January 20, 2010 3:17 PM
  • RE: "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."

    Hi Carlos,

    I assume the above quote is also true of System.Web.Configuration?

    The problem is that using Microsoft.Web.Administration requires that a schema.xml file be added to the "system32\inetsrv\config\schema" directory, which causes problems in any scenario where this isn't possible. Two examples: (1) some of our customers want to deploy BlueDragon in self-contained ASP.NET web applications without making any other modifications to their system (they don't want to run an installer); and, (2) Windows Azure, which doesn't support adding a schema.xml.

    Currently, we have an IIS7 Manager plug-in that uses Microsoft.Web.Administration (of course), as does the BlueDragon application. We'd like to modify BlueDragon to instead use System.Web.Configuration--particularly for Windows Azure--and, we'd like to use just one API within BlueDragon.

    Do you see our dilemna? Can you think of any solutions?

    This page talks about how to "View Inherited and Local Configuration Settings Programmatically", but my testing shows that it doesn't work for ApplicationHost.config:

    http://msdn.microsoft.com/en-us/library/ms228059(VS.100).aspx

    At this point we seem to be stuck without a good solution.

    Thanks,

    Vince Bonfanti
    New Atlanta Communications, LLC

    Friday, January 22, 2010 10:12 PM
  • Hi Vince, good to hear from you,
    so the statement is indeed true for System.Web.Configuration, only the IIS configuration system understands and knows about ApplicationHost.config.
    What we have seen in some of these cases is for developers to use System.Configuration derived classes (System.Configuration.ConfigurationSection) for the runtime and use Microsoft.Web.Administration for the administration side (IIS Manager).

    You are correct on highlighting the fact that certain scenarios are not possible for some deployment restrictions and that XML schema is required for MWA to work.

    A good example of this is WCF and the new Dublin Application Server http://www.microsoft.com/NET/Dublin.aspx which have leveraged Microsoft.Web.Administration and the IIS Manager APIs for their User interface and yet continue to use System.Configuration (System.Web.Configuration) for their runtime behavior. I should say that there are certain challenges on doing so, such as different encryption models, however if the sections are well designed it is the best approach that we have seen.
    Saturday, January 23, 2010 4:33 AM
  • Thanks. That's exactly the direction in which we're headed (MWA for the user interface and System.Web.Configuration--SWC--for runtime). We've noticed that SWC does support inheritance from web sites to web applications, and from machine.config and the root web.config down to web sites and web applications. It seems that if we could get our user interface code to write system level settings to the global web.config instead of applicationHost.config, that this would be the best solution; is there a way to do this? Do we lose anything by using the global web.config for system level settings rather than applicationHost.config?

    Also, we are currently encrypting one attribute of one element via MWA--do you have any advice (or references) on how to read this via SWC?

    Thanks for your help.

    Vince
    Wednesday, January 27, 2010 8:36 PM