2

I'm trying to get data from app.config and I always get zero. The App.config is here:

    <?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="ExplorerContext" connectionString="metadata=res://*/ExplorerData.csdl|res://*/ExplorerData.ssdl|res://*/ExplorerData.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=MYT\SQLEXPRESS;Initial Catalog=Explorer;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

Could someone explain what is wrong, why I cannot get the values, System.Configuration.ConfigurationManager.AppSettings.Count is always 0

I fogot to specify that I use class library, that I'm trying to check using NUnit project. And this class library calls one more project (class library too) that uses ADO.NET Entity Project.

bluish
  • 26,356
  • 27
  • 122
  • 180
mimic
  • 4,897
  • 7
  • 54
  • 93
  • I know this is old, if none of the answer here help try: http://stackoverflow.com/questions/4280970/configurationmanager-appsettings-getting-null – Jeremy Thompson Jul 25 '12 at 04:16

3 Answers3

7

You're not using AppSettings! Check ConfigurationManager.ConnectionStrings instead.

bluish
  • 26,356
  • 27
  • 122
  • 180
Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
1

You do not have any AppSettings declared in your config file. If you are trying to get the Connection strings then you should use:

var connectionString = ConfigurationManager.ConnectionStrings["ExplorerContext"].ConnectionString;

EDIT If you are using NUnit, you can supply a configuration file as long as it is named after the DLL. For example, if the DLL is named Foo.dll, then the configuration file must be named Foo.dll.config. Visual Studio will not do this for you for a DLL. You must create the file manually and you must ensure it gets into the proper bin folder.

See NUnit Configuration Files for more.

Thomas
  • 63,911
  • 12
  • 95
  • 141
  • Oh, my! When I use this construction I can see some strange connection string that I DON'T have in my app.config: System.Configuration.ConfigurationManager.ConnectionStrings[0] {data source=.\SQLEXPRESS;Integrated ... Name: "LocalSqlServer" ProviderName: "System.Data.SqlClient" I cannot imagine where is it from??? – mimic Mar 28 '10 at 07:04
  • 1
    I didn't specify ConnectionStrings[0], I pulled it by name using the name in your OP (i.e. ConnectionStrings["ExplorerContext"]. By default, ASP.NET does add a connection string in the machine.config file for LocalSqlServer which you can ignore. – Thomas Mar 28 '10 at 07:14
  • @user46503: that connection string to the local SQL server is from your machine.config deep in the bowels of your system..... – marc_s Mar 28 '10 at 07:19
  • When I try to call System.Configuration.ConfigurationManager.ConnectionStrings["ExplorerContext"] it always returns null! I cannot understand, why because I can see App.config and it copied into bin/debug folder... – mimic Mar 28 '10 at 07:21
1

I forgot to specify that I use class library, that I'm trying to check using NUnit project. And this class library calls one more project (class library too) that uses ADO.NET Entity Project.

You need to put your configuration information into the main app - the app using/calling your class library project with the EF model. .NET configuration does not natively support class library level app.config's.

So in your test environment, the main test harness will need to have these entries in its app.config.

If you insist that your class library assembly have its own config - check out Jon Rista's Cracking the Mysteries of .NET 2.0 Configuration where he explains in great detail how to use the ConfigurationManager.OpenExeConfiguration call to open any arbitrary *.config file and use it within the .NET 2.0 configuration system. It works - but it's more work and I wouldn't really recommend it.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459