On Understanding ConfigurationManager in .NET

I'm trying to understand ConfigurationManager in .NET by practicing it in different scenarios.

I have two projects: Project1 and Project2. Project2 uses Project1.

My Situation: I have a section(serializedfilename) in my app.config file in project1. and I have this line of code in a class1 of project1

 private static string SerializedConfiguration = ConfigurationManager.AppSettings["SerializedFilename"];

In project2, I call a method in the class1 of project1 and i'm getting an exception.

its like .NET doesnt seem to be able to find the settings in app.config file of project1 when called from project2 even though i'v referenced project1 appropriately.

However, when i move the section(serializedfilename) to app.config file of project2...everything seems to work well...

Can someone explain to me what is happening here? I'm tempted to assume that ConfigurationManager only exist in the context of the client application(in this case project2) and not in the original application(project1 in this case)

Note: i'm new(1month+ to .NET)

Thanks.


When you add a reference app.Config is not copied. App.config needs to be in your primary project, in your case project 2. if you add it in project 2 and call it in the referenced object Project 1, you will not get any error as VS will pick it from your primary project.


Correct, ConfigurationManager looks to the executing assembly for the app.config or web.config to lookup settings.

So if your class library DLL that accesses configuration settings is running under a web site project then the app settings will need to be added to web.config, not the app.config of the class library.

链接地址: http://www.djcxy.com/p/72942.html

上一篇: CakePHP:把'服务'逻辑放在哪里

下一篇: 在.NET中了解ConfigurationManager