PositiveGuy
PositiveGuy

Reputation: 47763

C# Projects and Config Files

I'm trying to pull some key/values in my class. This is a C# Class Library Project (not web project) so it's got an App.Config.

In my C# Project (Class Library), got the following:

1) A utility method to pull a key/value from a config file:

public class ConfigUtil
{
    /// <summary>
    ///  Utility method to retrieve configuration setting values.
    /// </summary>
    /// <param name="configKey">Configuration Settings key to retrieve.</param>
    /// <returns>empty string if key is null</returns>
    public static string GetAppConfigSetting(string configKey)
    {
        return ConfigurationManager.AppSettings[configKey] ?? string.Empty;
    }
}

This utility method was used in a web project and I just copied and pasted this nice helper into my C# Project because I need these auth and end points for authing to a third party API in some code in my C# Project.

2) A Config.cs to hold my lookups

class Config
{
    public static string SomeThirdPartyApiEndpoint
    {
        get { return ConfigUtil.GetAppConfigSetting("SomeThirdPartyApiEndpoint"); }
    }

    public static string SomeThirdPartyApiAuthUsername
    {
        get { return ConfigUtil.GetAppConfigSetting("SomeThirdPartyApiAuthUsername"); }
    }

    public static string SomeThirdPartyApiAuthPassword
    {
        get { return ConfigUtil.GetAppConfigSetting("SomeThirdPartyApiAuthPassword"); }
    }
}

3) In my App.Config, the key/values:

  <appSettings>
    <add key="SomeThirdPartyApiEndPoint" value="http://someEndPointUrl.com/XMLAPI" />
    <add key="SomeThirdPartyApiAuthUsername" value="someUserNameHere" />
    <add key="SomeThirdPartyApiAuthPassword" value="5233s54" />
  </appSettings>

4) Some example code in one of my classes in this project trying to pull one of the key values:

string password = Config.SomeThirdPartyApiAuthUsername;

this line is in one of my class methods.

The problem: it's not finding any of my key/values. When I debug, I get an empty string for password.

I assume I'm doing this right in that these key/values need to be put in my App.Config since I do not have a web.config since this is not a Web Project. This is a wrapper project I am creating to wrap calls to a third party API.

Another question related: So then would then ConfigurationManager.AppSettings look for an App.Config if a Web.config is not present? I am wondering how this ConfigurationManager pulls...does it look for only web.configs or any .config available in the immediate project?

Upvotes: 0

Views: 3124

Answers (3)

For any type of .Net application except silverlight the config values are retreived by using ConfigurationManager.AppSettings["key"] in C# and while in silverlight we have to read the config entries with the help of xmlreader. For more checking refer to ConfigurationManager.AppSettings["KEYNAME"] is Null

Upvotes: 1

Jeff S
Jeff S

Reputation: 7484

I believe that the ConfigurationManager.AppSettings looks in the configuration file of the running program. If this is a library project, you need to add the settings you want to the config file of the host.

Upvotes: 1

Skyrim
Skyrim

Reputation: 865

SomeThirdPartyApiUsername should be SomeThirdPartyApiAuthUsername


public static string SomeThirdPartyApiAuthUsername 
{ 
    get { return ConfigUtil.GetAppConfigSetting("SomeThirdPartyApiAuthUsername"); } 
} 

but your key is

"SomeThirdPartyApiUsername"

Upvotes: 0

Related Questions