Can someone provide a quick App.config/Web.config tutorial?

ぐ巨炮叔叔 提交于 2019-11-27 15:08:38

问题


I've used these two configuration files many times before, but I've never taken the time to fully understand how they really work. As most people do, I understand the basics in how to call WebConfigurationManager.AppSettings["key"] to get config values.

Here are some questions I came up with:

  1. What happens when you reference a configuration value within a class library, and the library is part of a bigger solution? Does the app.config need to be copied to the output directory in order for the variables to be found? (I assume yes)
  2. Can you directly use a configuration value from an app.config in another class library?
  3. Assuming question 3 is "yes", what happens if there are multiple app.config files from different libraries containing configuration values with the same key?
  4. What happens when you reference the web.config, but in a class library?
  5. What happens when you reference the app.config, but in a website or web application project?

回答1:


The underlying answer to all of your questions is the same: Unless you set up something unusual, all assemblies in your project will read from the same configuration file. In a web app, they will all read from "web.config". In any other project type, they will read from the starting assembly's config file.




回答2:


The app/ web.config that is used is the one that starts the process. Easier if I give an example:

  • Assume all projects in a solution have an app or web.config.
  • A test in project A calls code in project B that calls a web service in project C which calls code in project D.

In this case code in project A and B will use the app.config in project A. Code in project C and D will use the web.config in project C.



来源:https://stackoverflow.com/questions/1188681/can-someone-provide-a-quick-app-config-web-config-tutorial

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!