Search code examples
asp.net.netconfigurationappfabricconfigurationsection

How to read an Internal ConfigurationSection from Web.Config in ASP.NET


I am trying to read a Microsoft defined ConfigurationSection:

Microsoft.ApplicationServer.Caching.DataCacheClientSection

but the type is internal so I cannot access it in the conventional way.

i.e.

var dataCacheClientSection = (DataCacheClientSection)ConfigurationManager.GetSection("dataCacheClient");

I know I can use reflection but do I have any other options?


Solution

  • You can open the configuration file as XmlDocument and read whatever you want there using XPath.

    Something like:

    var xmlDoc = new XmlDocument();
    xmlDoc.Load(Assembly.GetExecutingAssembly().Location + ".config");
    XmlNode setting = xmlDoc.SelectSingleNode("configuration/...");
    

    Use the correct XPath in SelectSingleNode