Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot read app.config, why?

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.

like image 342
mimic Avatar asked Jul 04 '26 06:07

mimic


2 Answers

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

like image 191
Anthony Pegram Avatar answered Jul 06 '26 01:07

Anthony Pegram


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.

like image 22
marc_s Avatar answered Jul 06 '26 00:07

marc_s



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!