在Visual Studio中为C#项目管理多个配置文件

时间:2023-01-14 21:25:57

I have learned that builds should be very straight forward and requires less human intervention. In my situation, I have 3 environments, DEV (development version), SIT (STG integration testing) and UAT (user acceptance testing). All these environments have different database connection details and other settings (such as logging).

我了解到构建应该非常直接,并且需要较少的人为干预。在我的情况下,我有3个环境,DEV(开发版),SIT(STG集成测试)和UAT(用户验收测试)。所有这些环境都有不同的数据库连接详细信息和其他设置(例如日志记录)。

Currently when application needs to be deployed to SIT environment, what I need to do is to build the application. Install it on the target environment and modify the configuration file by hand. This is problematic as I may miss some entries. Things will get worse when configuration file is huge.

目前,当需要将应用程序部署到SIT环境时,我需要做的是构建应用程序。将其安装在目标环境中并手动修改配置文件。这是有问题的,因为我可能会错过一些条目。当配置文件很大时,情况会变得更糟。

Questions

问题

1 - Is there anyway to specify the environment name when making a build from VS. So that the configuration file for that environment will be used.

1 - 无论如何在从VS进行构建时指定环境名称。这样就可以使用该环境的配置文件。

2 - What are the other methods which will help to reduce human intervention in builds?

2 - 有哪些其他方法可以帮助减少人工干预?

3 - Assume my applications name is "Foo". I have a package project in the solution which will create installable package. Is there anyway to specify the product title depending upon the environment? I mean if SIT is selected, product name when installing will be "Foo-SIT". I guess this will avoid confusion of what environment version one is installing. I would be happy to hear any better alternatives if there are any.

3 - 假设我的应用程序名称为“Foo”。我在解决方案中有一个包项目,它将创建可安装的包。无论如何根据环境指定产品名称?我的意思是如果选择了SIT,安装时的产品名称将是“Foo-SIT”。我想这可以避免混淆版本的安装环境。如果有的话,我会很高兴听到任何更好的选择。

3 个解决方案

#1


1  

One way to handle this is to create 3 projects and put the environment in the name of the new projects. Then use a post-build event to copy the correct files based on the project name.

处理此问题的一种方法是创建3个项目并将环境放在新项目的名称中。然后使用post-build事件根据项目名称复制正确的文件。

Inside the post build event you can tell the project name based on the $(ProjectName) macro. So you can do things like

在post build事件中,您可以根据$(ProjectName)宏告诉项目名称。所以你可以做的事情

IF "$(ProjectName)"="devproject" (
   copy ...
   copy ...
)

It's best to make these projects defer most of the real work (compilation) to a single project so that you don't have to keep settings up to date across multiple projects.

最好让这些项目将大部分实际工作(编译)推迟到单个项目中,这样您就不必在多个项目中保持最新设置。

#2


2  

2 -- You're absolutely right -- you should attempt to automate your build and deployment process as much as possible. Your best bet is to set up an automated build / deploy script using something like NAnt. Your script can have targets for each of your environments, e.g.

2 - 你是绝对正确的 - 你应该尽可能地尝试自动化你的构建和部署过程。最好的办法是使用NAnt之类的东西设置自动构建/部署脚本。您的脚本可以包含每个环境的目标,例如

<project name="MyApp" xmlns="http://nant.sf.net/release/0.85/nant.xsd">
    <target name="DEV">
      <!-- execute any tasks needed to build the app, such as msbuild(1) -->

      <!-- execute any tasks needed to build the tweak the config files, such as xmlpeek / xmlpoke(2) -->

      <!-- execute any tasks needed to copy the now-built and configured project output the tweak the destination server, such as copy(3)-->
    </target>

    <target name="SIT">...</target>
    <target name="UAT">...</target>

</project>

If you're not familiar with NAnt, it should take you less than a day to get up and running and writing scripts. The payoff is immediate and long-lasting, as you'll never have to VNC onto a server and tweak configuration files ever again.

如果您不熟悉NAnt,那么启动和运行以及编写脚本应该花费不到一天的时间。收益是即时且持久的,因为您永远不必将VNC转换到服务器上并再次调整配置文件。

#3


2  

You can look at the EnvRide tool on codeplex.com. Its purpose its exactly to make it easier to managem multiple configuration files in an automated way.

您可以在codeplex.com上查看EnvRide工具。其目的正是为了更容易以自动方式管理多个配置文件。

#1


1  

One way to handle this is to create 3 projects and put the environment in the name of the new projects. Then use a post-build event to copy the correct files based on the project name.

处理此问题的一种方法是创建3个项目并将环境放在新项目的名称中。然后使用post-build事件根据项目名称复制正确的文件。

Inside the post build event you can tell the project name based on the $(ProjectName) macro. So you can do things like

在post build事件中,您可以根据$(ProjectName)宏告诉项目名称。所以你可以做的事情

IF "$(ProjectName)"="devproject" (
   copy ...
   copy ...
)

It's best to make these projects defer most of the real work (compilation) to a single project so that you don't have to keep settings up to date across multiple projects.

最好让这些项目将大部分实际工作(编译)推迟到单个项目中,这样您就不必在多个项目中保持最新设置。

#2


2  

2 -- You're absolutely right -- you should attempt to automate your build and deployment process as much as possible. Your best bet is to set up an automated build / deploy script using something like NAnt. Your script can have targets for each of your environments, e.g.

2 - 你是绝对正确的 - 你应该尽可能地尝试自动化你的构建和部署过程。最好的办法是使用NAnt之类的东西设置自动构建/部署脚本。您的脚本可以包含每个环境的目标,例如

<project name="MyApp" xmlns="http://nant.sf.net/release/0.85/nant.xsd">
    <target name="DEV">
      <!-- execute any tasks needed to build the app, such as msbuild(1) -->

      <!-- execute any tasks needed to build the tweak the config files, such as xmlpeek / xmlpoke(2) -->

      <!-- execute any tasks needed to copy the now-built and configured project output the tweak the destination server, such as copy(3)-->
    </target>

    <target name="SIT">...</target>
    <target name="UAT">...</target>

</project>

If you're not familiar with NAnt, it should take you less than a day to get up and running and writing scripts. The payoff is immediate and long-lasting, as you'll never have to VNC onto a server and tweak configuration files ever again.

如果您不熟悉NAnt,那么启动和运行以及编写脚本应该花费不到一天的时间。收益是即时且持久的,因为您永远不必将VNC转换到服务器上并再次调整配置文件。

#3


2  

You can look at the EnvRide tool on codeplex.com. Its purpose its exactly to make it easier to managem multiple configuration files in an automated way.

您可以在codeplex.com上查看EnvRide工具。其目的正是为了更容易以自动方式管理多个配置文件。