I have a class called LocalConstants....
public static class LocalConstants {
public static string DM_PATH = ConfigurationManager.AppSettings["DMQueue"];
public static string PROJECT_PATH = ConfigurationManager.AppSettings["MSQueue"];
}
When trying to access this class in my main program I am getting a null reference exception. Anything from ConfigurationManager.AppSettings[ is always null. But if I write
//The value is returned fine
string bo=ConfigurationManager.AppSettings["MSQueue"];
this compiles fine but is always null and throws a NullRefexception
string moomoo = LocalConstants.PROJECT_PATH;
The exception is The type initializer for 'TestCodeOutOnSide.LocalConstants' threw an exception.
The innerException is the basic Object reference not set to an instance of an object.
Even if I change the PROJECT_PATH to
public static readonly string PROJECT_PATH = @"FORMATNAME:DIRECT=OS:serus-nickl\RMQDEV";
I get the same exception
Any ideas?
You could add a reference to System. Configuration. dll and use the ConfigurationManager class. There is an example available on MSDN here: https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager(v=vs.110).aspx.
App. Config is an XML file that is used as a configuration file for your application. In other words, you store inside it any setting that you may want to change without having to change code (and recompiling). It is often used to store connection strings.
Locate the web. config file in the root directory of your application (or create one if it does not already exist). Add an <appSettings> element. Add <add> child elements along with key / value pairs to the <appSettings> element as required.
To begin with, if you are doing this to provide some sort of performance benefit then you should know that these are cached. See ConfigurationManager.AppSettings Caching, to remove any.
Second the issue is most likely that Static field initialization does not work how you expect it to. So your code as written provides no guarantee of that ConfigurationManager.AppSettings
has been run. From the linked article sample code:
might produce either the output:
Init A
Init B
1 1
or the output:
Init B
Init A
1 1
[EDIT per OP comment]
There must be something else involved as:
public static class LocalConstants
{
public static string DM_PATH = "DMQueue";
public static string PROJECT_PATH = "MSQueue";
}
class Program
{
static void Main(string[] args)
{
string moomoo = LocalConstants.PROJECT_PATH;
Console.WriteLine(moomoo);
}
}
works for me.
[Edit 2 - Fro those who come after]
It looks like The type initializer for ‘SomeClass’ threw an exception can be a case where
But when it's called by the WPF designer, the "application" is Visual Studio, which (presumably) doesn't have the appropriate connection strings in its .config file;
The fix for that author was:
moving the instantiation of my Entity Data Model into a property
Why don't try something like:
public static string ProjectPath
{
get
{
return ConfigurationManager.AppSettings["MSQueue"];
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With