ConfigurationManager.GetSection returns null
Asked Answered
R

3

17

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

Rora answered 8/8, 2012 at 21:4 Comment(6)
you're casting it incorrectly.I will post an example below along with a link you can use as a referenceNewhouse
If the as cast operation fails, it returns a null value. However, configuration file notes that the section type is NameValueSectionHandler -- you should be casting to this type when calling GetSection().Ankledeep
even when I change the type to NameValueSectionHandler, I'm still getting a null backRora
what is the value of nvc when you place a break point..?Newhouse
just to see what would happen, I just returned an object, like below but still got a null. object nvc = ConfigurationManager.GetSection("procedureList");Rora
Yes that cast was causing me errors. Instead of type="System.Configuration.NameValueSectionHandler, System, Version=4.0.30319, Culture=neutral, PublicKeyToken=b77a5c561934e089" I used type="System.Configuration.NameValueSectionHandler".Winding
N
6

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

Newhouse answered 8/8, 2012 at 21:10 Comment(6)
got it figured out. I had the app config in the dll, not the calling form. since ultimately the dll is going to be called by a service, I need to fix that with ConfigurationManager.OpenExeConfiguration. Thanks for the prodding.Rora
Not a problem.. that's why I always ask developers if they debug their code.. it's an awesome tool the Debugger... glad you got it workingNewhouse
Have you tried: NameValueCollection nvc = (NameValueCollection)ConfigurationManager.GetSection("procedureList"); ? I suggest this, because ConfigurationSettings.GetConfig is obsolete now :/Commonwealth
Okay wait, just local errors. It will go dead some day. Essential data would be better locally.Haymaker
I gave an example that works not sure why you down voted it but it's all good.. cheersNewhouse
@Rora Could you please elaborate on how you fixed this issue?Hysteresis
L
6

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

Lightman answered 28/10, 2015 at 8:20 Comment(0)
I
1

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

Iila answered 29/4, 2021 at 10:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.