Visual Studio 中特定于开发人员的 app.config/web.config 文件 [英] Developer specific app.config/web.config files in Visual Studio

查看:26
本文介绍了Visual Studio 中特定于开发人员的 app.config/web.config 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有几个 .NET 项目,我们将某些设置存储在配置文件中.

We have several .NET projects where we store certain settings in configuration files.

现在每个开发者都会有自己的配置文件,它们略有不同(连接本地数据库的连接字符串不同,WCF 端点等)

Now each developer will have their own configuration files that differ a little (different connection strings to connect to local databases, different WCF endpoints, etc.)

目前我们倾向于检查 app/web.config 文件并修改它们以满足我们的需要.

At the moment we tend to check out app/web.config files and modify them to suit our needs.

这会导致许多问题,因为有时有人会在从 TFS.

This leads to many problems since from time to time someone will check in their own settings or loose custom configuration when getting latest version from TFS.

遇到这种情况你是怎么处理的?或者你根本没有这个问题?

How do you deal with situations like this? Or don't you have this problem at all?

推荐答案

我们使用的系统结合了此页面上的几个现有答案,并借鉴了 Scott Hanselman 提出的这个建议.

We use a system that combines several of the existing answers on this page, plus draws on this suggestion by Scott Hanselman.

简而言之,我们所做的是拥有一个通用的 app.config/web.config,并在单个文件中包含大多数特定设置,正如这里的其他答案所建议的那样.例如对于我们的 SMTP 设置,app.config 包含

In short, what we did was to have a common app.config / web.config, and to have most of the specific settings in individual files, as suggested by other answers here. e.g. for our SMTP settings, the app.config contains

<system.net>
  <mailSettings>
    <smtp configSource="config\smtp.config" />
  </mailSettings>
</system.net>

此文件在源代码管理中.但是,像这样的单个文件不是:

This file is in source control. However, the individual files, like this, are not:

<?xml version="1.0" encoding="utf-8" ?>
<smtp deliveryMethod="Network">
  <network host="127.0.0.1" port="25" defaultCredentials="false" password="" userName ="" />
</smtp>

这还不是故事的结局.新的开发人员或全新的源代码安装呢?大部分配置不再受源代码控制,手动构建它们需要的所有 .config 文件很痛苦.我更喜欢至少可以直接编译的源代码.

That's not quite where the story ends though. What about new developers, or a fresh source installation? The bulk of the configuration is no longer in source control, and it's a pain to manually build all of the .config files they need. I prefer to have source that will at least compile right out of the box.

因此,我们确实在源代码管理中保留了一个名为 .config.default 文件的 .config 文件版本.因此,一个新的源代码树看起来像这样:

Therefore we do keep a version of the .config files in source control, named .config.default files. A fresh source tree therefore looks like this:

仍然对开发人员没有任何用处,因为对于 Visual Studio 来说,它们只是毫无意义的文本文件.因此,批处理文件 copy_default_config.bat 负责从 .config.default 文件创建一组初始 .config 文件:

Still, not really any use to the developer, since to Visual Studio they're just meaningless text files. Hence the batch file, copy_default_config.bat, takes care of creating an initial set of .config files from the .config.default files:

@echo off
@REM Makes copies of all .default files without the .default extension, only if it doesn't already exist. Does the same recursively through all child folders.
for /r %%f in (*.default) do (
    if not exist "%%~pnf" (echo Copying %%~pnf.default to %%~pnf & copy "%%f" "%%~pnf" /y)
)
echo Done.

脚本可以安全地重新运行,因为已经拥有 .config 文件的开发人员不会覆盖它们.因此,可以想象将此批处理文件作为预构建事件运行..default 文件中的值对于新安装可能不完全正确,但它们是一个合理的起点.

The script is safely re-runnable, in that developers who already have their .config files will not have them overwritten. Therefore, one could conceivably run this batch file as a pre-build event. The values in the .default files may not be exactly correct for a new install, but they're a reasonable starting point.

最终每个开发人员得到的是一个配置文件文件夹,看起来像这样:

Ultimately what each developer ends up with is a folder of config files that looks something like this:

这可能看起来有点令人费解,但绝对比开发人员相互踩踏的麻烦要好.

It may seem a little convoluted, but it's definitely preferable to the hassle of developers stepping on each other's toes.

这篇关于Visual Studio 中特定于开发人员的 app.config/web.config 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆