需要一种方法来控制 app.config 文件

发布于 2024-12-11 12:34:12 字数 962 浏览 1 评论 0原文

目前我们有 4 个人正在研究解决方案。该解决方案由一个中央可执行项目(Windows 服务)和许多类库解决方案组成。 Windows 服务有一个 app.config 文件,但由于某种原因,它的“复制到输出目录”设置被设置为“不复制”,因此我们可以在本次讨论中忽略它。

有两个不同的类库项目具有 app.config 文件。在其中一个项目中,它被复制到 bin\Debug 文件夹中作为 MyClassLibrary.dll.config,而另一个 DLL 的 bin\Debug 文件夹中同时包含 MyOtherClassLibrary.dll.config 和 app.config。我不知道这是怎么发生的。

这些是解决方案中的所有 app.config 文件。

解决方案中有一个MSTest项目。它有几个带有 [TestClass] 属性的类,每个类都在自己的 .cs 文件中。当我查看该项目的 bin\Debug 文件夹时,我在其中找到了 MyClassLibrary.dll 和 App.config,但没有 MyOtherClassLibrary.dll。这很奇怪,但这就是我所看到的。

如您所知,当我去运行其中一项测试时,文件将被复制到解决方案的 TestResults 文件夹中的特殊测试文件夹中。当我查看该文件夹时,我只找到一个 MyTestProject.dll.config 文件。当我打开它时,它的内容是 app.config 文件的内容。

这是怎么回事?我认为 Visual Studio 将所有配置文件合并在一起? MyClassLibrary.dll.config 文件中缺少设置(显然)会破坏依赖它的代码。

我们该如何解决这个问题?

托尼

编辑:

我刚刚仔细检查了 MyClassLibrary bin\Debug 文件夹 &同时具有 app.config 和MyClassLibrary.dll.config,所以我想我在 MyOtherClassLibrary 中看到的内容是正常的。我们只需要一种方法来管理所有这些设置。

There are 4 of us working on a solution right now. The solution consists of a central executable project (a Windows service) and a number of Class Library solutions. The Windows service has an app.config file, but the Copy to Output Directory setting for it is set to "Do not copy" for some reason, so we can ignore it for this discussion.

There are two different Class Library projects that have app.config files. In one of the projects, this gets copied into the bin\Debug folder as MyClassLibrary.dll.config, while the other DLL's bin\Debug folder has both a MyOtherClassLibrary.dll.config AND app.config in it. I'm not sure how that happened.

These are all of the app.config files in the solution.

There is an MSTest project in the solution. It has several classes with the [TestClass] attributes on them, each in their own .cs file. When I look at this project's bin\Debug folder, I find the MyClassLibrary.dll and App.config in it, but no MyOtherClassLibrary.dll. This is odd, but that's what I see.

When I go to run one of these tests, as you know, files get copied into a special test folder in the solution's TestResults folder. When I look in that folder, I find only a MyTestProject.dll.config file. And when I open this, its contents are those of the app.config file.

What's going on here? I thought Visual Studio merged all of the config files together? The lack of the settings from the MyClassLibrary.dll.config file (obviously) breaks the code that relies on it.

How do we fix this?

Tony

EDIT:

I just double checked the MyClassLibrary bin\Debug folder & that has both app.config & MyClassLibrary.dll.config, so I guess what I was seeing in MyOtherClassLibrary is normal. We just need a way to manage all of these settings.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

行雁书 2024-12-18 12:34:12

不,VS 不会将配置文件合并在一起。它不会这样做,因为可能有一个解决方案可以生成两个或多个可执行文件作为项目。即使对于最终总是作为类库并且只有一个可执行文件的项目,由于关注点分离,合并 app.configs 也不是一个好主意;项目库应该是一个“黑匣子”,引用代码只需要通过实例化方法和调用方法的外部接口即可。根据定义,app.config 及其信息是一个实现细节,只有需要它的项目才应该知道,而不是引用它的项目。此外,如果您独立于另一个可执行文件重新编译项目库并推出新的 DLL,那么配置文件将如何合并?

如果一个项目有 app.config,则会专门为该项目创建一个配置,该配置与所有其他项目不同且独立。

如果您的配置在公共部分中具有公共数据,则可以通过为这些部分指定外部配置文件并使用“configSource”属性从每个项目的“主”配置文件引用它来最大程度地减少冗余数据量。

No, VS does NOT merge config files together. It does not do this because it is possible to have a solution that generates two or more executables as projects. Even for projects that always end up as class libraries and there's only ever one executable, it's not a good idea to merge app.configs because of separation of concerns; the project library should be a "black box" that referencing code only needs an external interface to via instantiating methods and calling methods. An app.config and its information is by definition an implementation detail, which only the project that needs it should know, and NOT the projects that reference it. Besides, if you recompiled the project library independently from another executable and just pushed out the new DLL, how would the config files be merged then?

If a project has an app.config, a config will be created for that project specifically, distinct and separate from all others.

If your configs have common data in common sections, you can minimize the amount of redundant data by specifying an external config file for those sections and referencing it from each project's "main" config file using "configSource" attributes.

仅一夜美梦 2024-12-18 12:34:12

您应该只需要一个 配置文件,并且它可以是正在执行的项目(即控制台项目、Web 应用程序项目等)的一部分。如果类库之一中的类引用配置值,则它应该可以从执行项目中访问该配置值/作用域。我从来没有见过任何自动合并。

You should only need one config file and it can be part of the executing project (i.e. console project, web application project, etc). If your class from one of the class libraries is referencing a config value, it should have access/scope to it from the executing project. I have never seen any automatic merging.

月光色 2024-12-18 12:34:12

您将设置拉入主配置或实现一些在构建时合并某些设置的内容,我在这里以某种相关的方式谈到这一点:
Visual studio 2010 - 每个开发人员/计算机/环境 Web。配置设置 默认

情况下,您不会在主配置之外自动复制任何内容,它不会那样工作。

You pull your settings into your main config or implement something that merges some on build, I touch upon this in a somewhat related way here:
Visual studio 2010 - Per Developer/machine/environment Web.Config settings

You will not get anything automatically copied by default outside of your main config, it doesn't work that way.

烟火散人牵绊 2024-12-18 12:34:12

请记住,像这样的配置文件(包括 web.config 和 app.config 文件)会发生两件事:

  1. 按原样部署文件。它可能会进行转换,但文件会按原样部署。

  2. 它被编译为属于程序集。因此,我名为 ProjectOne 的项目将对其 App.Config 文件进行编译并重命名为 ProjectOne.dll.config(假设我的 ProjectOne 程序集是“ ProjectOne")

这很重要,因为它完全与范围有关。此重命名是设计使然,因为它仍然允许项目程序集首先访问其自己的配置文件。这些将优先于方案 1 中的实际 app.config。

您可以通过更改文件的属性来防止方案 2 发生,以便 MSBuild 不会触及该文件。然后它可能会尝试按原样部署它。在这种情况下,它将根据构建顺序覆盖任何先前部署的。

Just remember that there is two things that happen with a config file like this (including web.config and app.config files):

  1. You deploy the file as is. It may be subject to transforms but the file is deployed with the name as it is.

  2. It gets compiled as belonging to the assembly. So my project named ProjectOne will have it's App.Config file compiled and renamed as ProjectOne.dll.config (assuming my assembly for ProjectOne is "ProjectOne")

This is important because its all about scope. This renaming is by design as it still allows a project assembly to access its own config file first. These will take priority over those of the actual app.config from scenario 1.

You can prevent scenario 2 from occurring by changing the properties of the file so that MSBuild does not touch the file. Then it may attempt to deploy it as is. In this situation it will overwrite any previously deployed ones based on build order.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文