2

Reading keys from app.config is not working

I have 4 layers in my solution:

  • Business Layer
  • Data Access Layer
  • Presentation Layer
  • Shared Layer

I added this app.config file in the shared layer:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="TEST" value="ItsWorking" />
  </appSettings>
</configuration>

and I included the System.Configuration dll in references

I tried

var value = ConfigurationManager.AppSettings["TEST"]; 

but it returns null

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Shalto
  • 73
  • 1
  • 8

1 Answers1

1

The configuration only reads from the executing project assembly so you only need in the executable project and you can access from all your others projects.

Maybe you can use a configurationProvider with an Interface and with IOC you can access from all your projects.

Like

public interface IConfigurations
{
  string TEST {get;}
}

You use this interfaces in all your projects and only where you implement the properly configurations you implement the interface for a app.config, xml, .text, DB, or any source.

  public class Configurations : IConfigurations
    {
      public string TEST => ConfigurationManager.AppSettings["TEST"]; 
    }
Dei Revoledo
  • 175
  • 1
  • 5