Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConfigurationManager.GetSection returns null

Tags:

c#

app-config

Here is my app.config

<configuration>
  <configSections>
      <section name="procedureList" type="System.Configuration.NameValueSectionHandler, System, Version=4.0.30319, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
  </configSections>

  <procedureList>
    <add key="NAS.spBusObjGetLineProd" value="@area='Melt Shop';@endDt=?date?;@dayonly=1;@obj='Melt Shop Business Objective" />
    <add key="NAS.spBusObjGetLineProd" value="@area='Cold Mill';@endDt=?date?;@dayonly=1;@obj='Cold Mill Business Objective" /> 
  </procedureList>
  <appSettings>
    <add key="Connstr" value=""/>
    <add key="Userid" value=""/>
    <add key="Timeout" value=""/>
  </appSettings>

</configuration>

But when I call it in code, I'm getting a null back

public void samplemethod()
{
    NameValueCollection nvc = ConfigurationManager.GetSection("procedureList") as NameValueCollection;
    string[] keys = nvc.AllKeys;
}

I would appreciate any help pointing out what I've done wrong

like image 832
edepperson Avatar asked Aug 08 '12 21:08

edepperson


3 Answers

Using section handlers to group settings in the configuration file

For example you can follow something like the following

private void ReadSettings()
{
    NameValueCollection loc = 
   (NameValueCollection )ConfigurationSettings.GetConfig("procedureList");
}

MSDN ConfigurationManager.GetConfig Method

like image 129
MethodMan Avatar answered Oct 11 '22 16:10

MethodMan


If you are testing your class you must copy the configuration to the app.config in your Test project.

like image 39
David Soler Avatar answered Oct 11 '22 17:10

David Soler


using immediate window check which config file it is pointing to. in my case i had app.config which i am expecting it to read, but on using the command. ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) it is pointing to something else like nuintrunner.exe.config as that info is loaded into bin. this help in loading the right configuration file

like image 25
Ajay Kopperla Avatar answered Oct 11 '22 17:10

Ajay Kopperla