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

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

问题描述

我们有几个.NET项目,我们在配置文件中存储某些设置。
现在每个开发人员都有自己的配置文件,它们有所不同(不同的连接字符串连接到本地数据库,不同的WCF端点等)。

We have several .NET projects where we store certain settings in config files. Now each developer will have their own config files that differ a little (different connection strings to connect to local db, different WCF endpoints etc.)

我们倾向于查看app / web.config文件并修改它们以适应我们的需要。
这会导致许多问题,因为有人会不时检查自己的设置或从tfs获取最新版本时松开自定义配置。

At the moment we tend to check out app/web.config files and modify them to suit our needs. This leads to many problems since from time to time someone will check in their own settings or loose custom config when getting latest version from tfs.

我的问题是:你怎么处理这样的情况?

My question is: how do you deal with situations like this? Or you don't 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文件,命名为 .config.default 文件。因此,新的源代码树如下所示:

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天全站免登陆