开发者

Cannot read app.config, why?

开发者 https://www.devze.com 2022-12-24 17:19 出处:网络
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\"?>

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"?>
<configura开发者_如何学Ction>
  <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.


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号