多个应用程序共享相同的配置文件

发布于 2024-08-11 04:25:30 字数 487 浏览 5 评论 0原文

在我的生产环境中,我在同一台计算机上安装了多个 Web 应用程序。这些应用程序访问各种 Web 服务和两个 SQL Server 数据库,这意味着我的 web.config 文件非常大并且充满详细信息。除此之外,我还有 Windows 服务,它们使用相同的 Web 服务并访问不同的数据库,而且我还有包含大量详细信息的大型 app.config 文件。整个解决方案是使用.NET 3.5 (C#) 开发的。

当我的四个开发团队之一想要部署解决方案某些部分的新版本并且所有配置文件都需要更改时,我的问题就开始了。因为我的 QA 环境与生产环境具有不同的计算机名称,所以我无法复制配置文件,我需要手动更改每个配置文件。而且,无论我的同事做了多少次检查(甚至使用检查列表),某些配置都会保持不变,并且解决方案的某些部分会停止。

有没有办法为所有应用程序创建唯一的配置文件?或者,至少将数据库连接字符串和 Web 服务 URL 集中在一个文件中以供所有应用程序使用?

PS:我无法更改部署团队,因为他们属于我的客户:(

In my production environment I have several Web applications installed in the same machine. These applications access various web services and two SQL Server databases this means that my web.config files are very big and full of details. Besides this I also have windows services that consume the same web services and access different databases and, again, I have big app.config files with lots of details. The entire solution was developed using .NET 3.5 (C#).

My problem begins when one of my four development team wants to deploy a new version of some part of solution and all configuration files need to be changed. Because my QA environment have different machine names from the production environment I can’t do a simple copy of configuration files I need manually change each one. And, no matter how many checks my colleagues do (even using check lists), some config is left unchanged and some part of the solution stops.

Is there a way to create a unique config file for all applications? Or, at least, centralize the data base connection strings and web services URL in a file to be used by all applications?

P.S.: I can’t change the deployment team because they belongs to my customer :(

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

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

发布评论

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

评论(5

↙温凉少女 2024-08-18 04:25:30

您可以使用 connectionStrings 部分中的 configSource 属性来包含外部连接字符串文件:

Application web.config

<configuration>
  <connectionStrings configSource="connectionStrings.config" />
</configuration>

connectionStrings.config

<connectionStrings>
  <add name="connection1" connectionString="..." />
</connectionStrings>

注意:as marc_s在注释中建议,configSource 属性可以在任何配置部分使用。

You can use the configSource attribute in the connectionStringssection to include an external connection strings file:

Application web.config

<configuration>
  <connectionStrings configSource="connectionStrings.config" />
</configuration>

connectionStrings.config

<connectionStrings>
  <add name="connection1" connectionString="..." />
</connectionStrings>

Note: as marc_s suggests in the comments, the configSource attribute can be used on any config section.

弥繁 2024-08-18 04:25:30

如果我理解正确的话,你想考虑一些“稳定”的设置

  1. 配置系统是分层的,你可以将默认设置存储在
    C:\Windows\Micorsoft.NET\\Machine.configWeb.config
    但这是一个相当沉重的工具。出错,大多数 .NET 应用程序将无法启动

  2. 使用 ConfiguratonManager 类 从共享路径获取设置。这可能需要更改读取设置的(所有)代码。

If I understand correctly you want to factor out some 'stable' settings

  1. The config system is hierarchical, you can store default settings in
    C:\Windows\Micorsoft.NET\<version>\Machine.config or Web.config
    That is a pretty heavy tool though. Make an error and most .NET apps will fail to start

  2. Use ConfiguratonManager class to get settings from a shared path. That may require changing (all) code that reads the settings though.

瞄了个咪的 2024-08-18 04:25:30

您可以将外部 xml 文件包含到 web.config 中。

查看此问题< /a>

<!-- SomeProgram.exe.config -->
<configuration>
  <connectionStrings configSource="externalConfig/connectionStrings.config"/>
</configuration>

<!-- externalConfig/connectionStrings.config -->
<connectionStrings>
  <add name="conn" connectionString="blahblah" />
</connectionStrings>

You can include external xml files into your web.config.

See this question

<!-- SomeProgram.exe.config -->
<configuration>
  <connectionStrings configSource="externalConfig/connectionStrings.config"/>
</configuration>

<!-- externalConfig/connectionStrings.config -->
<connectionStrings>
  <add name="conn" connectionString="blahblah" />
</connectionStrings>
走过海棠暮 2024-08-18 04:25:30

这是另一个想法(实际上是我们如何解决配置问题)。也许这更适合您的需求。

我们的配置文件也相当大,但唯一真正改变的是配置字符串。

我们为每个配置都有一个文件,其中包含特定的配置字符串(用于开发的数据库、用于构建的数据库、用于 QA 的数据库)。这些文件如下所示:

<templates>
  <template key="db" value="development server connection string"/>
  <template .../>
</templates>

这些文件以它们使用的环境命名(devel.xml、build.xml、qa.xml

在配置文件中,连接字符串被占位符替换( ${db})。

我们为每个环境(开发、构建、质量保证)提供了构建配置。构建此类配置时,构建后事件会将配置文件中的占位符替换为模板文件中存储的值。这将为每个环境生成正确的配置文件。

这个想法取自使用预构建事件管理多个配置文件环境,但扩展为模板替换。

Here is another idea (actually how we solve our config problem). Maybe this fits your needs better.

Our config files are rather big too but the only thing that really changes are the configuration strings.

We have a file for each configuration which contains the specific configuration string (database for development, database for build, database for QA). The files looks like this:

<templates>
  <template key="db" value="development server connection string"/>
  <template .../>
</templates>

These files are named after the environment they are used in (devel.xml, build.xml, qa.xml)

In the configuration files the connection strings are replaced by place holders (${db}).

We have build configurations for each environment (devel, build, qa). When building such a configuration a post build event replaces the place holders in the config files with the values stored in the template files. This generates the correct config files for each environment.

The idea is taken from Managing Multiple Configuration File Environments with Pre-Build Events but extended with the template replacement.

人心善变 2024-08-18 04:25:30

您可以集中部分配置并动态加载该文件。请参阅这篇 codeproject 文章

You could centralize parts of the configuration and dynamically load that file. See this codeproject article

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