C# 您如何管理大型应用程序的 .NET app.config 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/89245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 11:30:09  来源:igfitidea点击:

How do you manage .NET app.config files for large applications?

提问by Dave Moore

Suppose a large composite application built on several foundation components packaged in their own assemblies: (database reading, protocol handlers, etc.). For some deployments, this can include over 20 assemblies. Each of these assemblies has settings or configuration information. Our team tends to like the VS settings editor (and the easy-to-use code it generates!), and the application vs. user distinction meets most of our needs.

假设一个大型复合应用程序构建在多个基础组件上,这些组件打包在它们自己的程序集中:(数据库读取、协议处理程序等)。对于某些部署,这可能包括 20 多个程序集。每个程序集都有设置或配置信息。我们的团队倾向于喜欢 VS 设置编辑器(以及它生成的易于使用的代码!),并且应用程序与用户的区别满足了我们的大部分需求。

BUT....

但....

It is very tedious to copy & paste the many configuration sections into our application's .xml. Furthermore, for shared components that tend to have similar configurations across applications, this means we need to maintain duplicate settings in multiple .config files.

将许多配置部分复制并粘贴到我们应用程序的 .xml 中非常繁琐。此外,对于跨应用程序往往具有相似配置的共享组件,这意味着我们需要在多个 .config 文件中维护重复的设置。

Microsoft's EntLib solves this problem with an external tool to generate the monster .config file, but this feels klunky as well.

微软的 EntLib 用一个外部工具来解决这个问题来生成怪物 .config 文件,但这感觉也很笨拙。

What techniques do you use to manage large .NET .config files with sections from multiple shared assemblies? Some kind of include mechanism? Custom configuration readers?

您使用什么技术来管理包含来自多个共享程序集的部分的大型 .NET .config 文件?某种包含机制?自定义配置阅读器?

FOLLOWUP:

跟进:

Will's answerwas exactly what I was getting at, and looks elegant for flat key/value pair sections. Is there a way to combine this approach with custom configuration sections?

Will 的回答正是我所得到的,并且对于平面键/值对部分看起来很优雅。有没有办法将这种方法与自定义配置部分结合起来?

Thanks also for the suggestions about managing different .configs for different build targets. That's also quite useful.

还要感谢有关为不同构建目标管理不同 .configs 的建议。这也很有用。

Dave

戴夫

采纳答案by Dave Moore

You use one master config file that points to other config files. Here's an example of how to do this.

您使用一个指向其他配置文件的主配置文件。 下面是如何执行此操作的示例。



In case the link rots, what you do is specify the configSourcefor a particular configuration section. This allows you to define that particular section within a separate file.

如果链接失效,您要做的是为特定配置部分指定configSource。这允许您在单独的文件中定义该特定部分。

<pages configSource="pages.config"/>

which implies that there is a file called "pages.config" within the same directory that contains the entire <pages />node tree.

这意味着在包含整个<pages />节点树的同一目录中有一个名为“pages.config”的文件。

回答by core

We created an AssemblySettingsConfig class that acts like ConfigurationManager, but loads a .config for each individual assembly. So the application has a .config, and any DLLs it references have their own .config files. Has worked out well so far.

我们创建了一个 AssemblySettingsConfig 类,它的作用类似于 ConfigurationManager,但为每个单独的程序集加载一个 .config。所以应用程序有一个 .config,它引用的任何 DLL 都有自己的 .config 文件。到目前为止效果很好。

回答by Wayne

A great way to manage large sets of configuration is to create custom configuration sections. Phil Haack discusses this very nicely in this article Custom configuration sections in 3 easy steps

管理大量配置的一个好方法是创建自定义配置部分。Phil Haack 在这篇文章中通过 3 个简单的步骤自定义配置部分很好地讨论了这一点

回答by sontek

My preferred method is to use MSBuild, if you right click on a project and click 'unload' a new menu option will pop up that says 'edit '. Select that and it'll open up the project file so you can edit it, scroll down until you find a commented out section that is called "AfterBuild".

我的首选方法是使用 MSBuild,如果您右键单击一个项目并单击“卸载”,则会弹出一个新的菜单选项,显示“编辑”。选择它,它将打开项目文件,以便您可以对其进行编辑,向下滚动直到找到一个名为“AfterBuild”的注释掉部分。

You can then add something like:

然后,您可以添加如下内容:

<Target Name="AfterBuild">
    <Delete Files="$(TargetDir)$(TargetFileName).config" />
    <Copy SourceFiles="$(ProjectDir)$(Configuration).config" DestinationFiles="$(TargetDir)$(TargetFileName).config" />
</Target>

This will replace the application config with one named [Release|Debug]app.exe.config. So you can maintain separate configurations depending on how the project is built.

这将使用名为 [Release|Debug]app.exe.config 的配置替换应用程序配置。因此,您可以根据项目的构建方式维护单独的配置。

But a quick and dirty option (if you don't want to play with msbuild) is to just maintain separate config files and then define which one you want to include, like this:

但是一个快速而肮脏的选择(如果您不想使用 msbuild)是只维护单独的配置文件,然后定义要包含的配置文件,如下所示:

<appSettings configSource="Config\appSettingsDebug.config"/>
<roleManager configSource="Config\roleManagerDebug.config"/>

And if you are doing an asp.net application, microsoft provides a great utility called "Web Deployment Projects" that will allow you to manage all of this easily, click here

如果你正在做一个 asp.net 应用程序,微软提供了一个叫做“Web 部署项目”的很棒的实用程序,它可以让你轻松管理所有这些,点击这里

回答by Geir-Tore Lindsve

Set up build configurations for each of your deployment/testing environment and use separate config files based on each build configuration.

为每个部署/测试环境设置构建配置,并根据每个构建配置使用单独的配置文件。

ScottGu has a nice postabout this and it works great. The only quirk we have is that we need to make sure that the config files (web.config) are checked out for edit from TFS before each build so that it can be copied over.

ScottGu 有一篇关于这个的好帖子,效果很好。我们唯一的怪癖是我们需要确保在每次构建之前从 TFS 检出配置文件 (web.config) 以进行编辑,以便可以复制它。