Access to constants Static class These links Application settings file

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 I try to access this class in my main program, I get an exception for null reference. Everything from ConfigurationManager.AppSettings [is always null. But if I write

  //The value is returned fine string bo=ConfigurationManager.AppSettings["MSQueue"]; 

this one compiles fine, but always null and throws a NullRefexception

  string moomoo = LocalConstants.PROJECT_PATH; 

An exception is the Type Initializer for "TestCodeOutOnSide.LocalConstants" made an exception.

An internal exception is the main Object reference not set to the object instance.

Even if I changed PROJECT_PATH to

 public static readonly string PROJECT_PATH = @"FORMATNAME:DIRECT=OS:serus-nickl\RMQDEV"; 

I get the same exception

Any ideas?

+6
source share
4 answers

I called it

 public static string Environment = AppEnvironmentVariable.ToUpper() != "PROD" ? "***FROM " + AppEnvironmentVariable.ToUpper() + "** " : ""; 

Before that

 public static string AppEnvironmentVariable = "DEV"; 

In the LocalConstants file, which broke it due to what Josh said about static field initialization

0
source

For starters, if you do this to provide some sort of performance advantage, then you should know that they are cached. For more information, see Caching ConfigurationManager.AppSettings .

Secondly, the problem is that Static field initialization does not work as you expect. Thus, your written code does not guarantee that ConfigurationManager.AppSettings been started. From the sample code for the related article:

can give either an output:

 Init A Init B 1 1 

or conclusion:

 Init B Init A 1 1 

[EDIT to OP comment]

There should be something else here:

 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 an example when

But when it is called by the WPF designer, the "application" is Visual Studio, which (presumably) does not have the corresponding connection lines in its .config file;

Correction for this author:

moving an instance of my entity data model to a property

+5
source

You can try to make them readonly

 public static readonly string PROJECT_PATH = ConfigurationManager.AppSettings["MSQueue"]; 

readonly fields can be lazily loaded

0
source

Why not try something like:

  public static string ProjectPath { get { return ConfigurationManager.AppSettings["MSQueue"]; } } 
0
source

Source: https://habr.com/ru/post/915833/


All Articles