none
Where is ConfigurationManager pulling GetSection() values from? RRS feed

  • Question

  • Hi all;

    I have the following call:

    var coll = (NameValueCollection)ConfigurationManager.GetSection("WindwardReports");

    When I look at coll in the debugger, it lists 3 keys. Two of those keys do not exist anywhere on my computer. I didn't just look in the config files (machine as well as app.config), I did a grep of every file on my computer and those text strings don't exist.

    How can I determine where .NET is pulling those from? This is a Word AddIn (and yes I looked at winword.exe.config too). What's weird is it does not return the values set there.

    thanks - dave


    What we did for the last 6 months - Made the world's coolest reporting & docgen system even more amazing

    Thursday, August 30, 2018 11:24 PM

All replies

  • Hi David,

    Based on my experience, the GetSection method reads the configSections node which is a special node in the Web.config configuration file that must be placed at the first node, that is, no other type of node can precede it. The configSections child node has a section and a section group, which is the collection node for the former:

    <configSections>
        <section name="CustomConfig" type="OrderMvc.CustomConfig, OrderMvc"/>
    </configSections>

    And Web. config about Customconfig definition:

     <CustomConfig>
        <Name Value="asdf"/>
      </CustomConfig>


    Here's how it works by Configurationmanager.getsection (...) Method call, if a class inherits the IConfigurationSectionHandler interface, it triggers the Create method of this interface, so that we can do something about it.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Configuration;
    using System.Xml;
    
    namespace OrderMvc
    {
        public class CustomConfig : IConfigurationSectionHandler
        {
            public string Name { get; private set; }
            public object Create(object parent, object configContext, XmlNode section)
            {
                CustomConfig config = new CustomConfig();
                var name = section.SelectSingleNode("Name");
                if (name != null && name.Attributes != null)
                {
                    var attribute = name.Attributes["Value"];
                    if (attribute != null)
                        config.Name = attribute.Value;
                }
                return config;
            }
        }
    }

    Call:

    var config = ConfigurationManager.GetSection("CustomConfig") as CustomConfig;
                Debug.WriteLine(config.Name);

    In short, you can try add some nodes(like WindwardReports) in your Web.config file and test read it by above steps.

    Helpfully it helps you.

    Best Regards,

    Simon


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Friday, August 31, 2018 12:25 PM
    Moderator
  • Hi;

    Yes, I'm aware of how it is supposed to work. However, in this case (app.config, not web.config) it is not pulling it from winword.exe.config. Or any other config file. The keys it is returning don't exist in any config file on my system.

    thanks - dave


    What we did for the last 6 months - Made the world's coolest reporting & docgen system even more amazing

    Saturday, September 1, 2018 12:52 PM
  • Hi David,

    Sorry for late response. This problem is really difficult to diagnosed since I don't know exactly what your project is. But in my experience the problem should be in the config file, can you try to delete some codes(App.config), just keep one node of App.config? Or re-create a project with the App.config in the old project to check issue ? Step by step troubleshooting.

    It's just personal advice. Hopefully it helps you.

    Best Regards,

    Simon


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Monday, September 3, 2018 4:54 AM
    Moderator
  • Hi David,

    This thread has been around for a long time. Did you solved your issue or any updates for this? if your issue is still exist then let us know about that, we will try to suggest you further to solve the issue.

    I’m eager to receive your feedback.

    Regards,

    Simon



    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Thursday, September 27, 2018 1:56 AM
    Moderator
  • Hi Simon;

    Thank you for checking in. Yes it's still a problem. Not only does it not return values set in the app.config file, it is returning 3 values and two of those values I did a grep for on my system and those strings exist in .cs files (where it queries them) but not in any other file.

    This is happening on just one system.

    Any idea how to find out where it is reading them from?

    thanks -d ave


    What we did for the last 6 months - Made the world's coolest reporting & docgen system even more amazing

    Thursday, September 27, 2018 5:46 PM