用于C#控制台应用程序的App.config文件的Web.config样式转换? [英] Web.config-style transforms for App.config files for C# Console apps?

查看:172
本文介绍了用于C#控制台应用程序的App.config文件的Web.config样式转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的团队爱上了发布个人资料.我们拥有许多测试环境,使用Web.config转换,我们可以替换每种环境中的计算机名称,配置设置等,这使我们的构建/部署/测试过程更加轻松.

My team is in love with publish profiles. We have a number of testing environments that we work in, and using Web.config transforms we're able to substitute the names of machines in each environment, configuration settings, etc, which makes our build/deploy/test process much easier.

例如:

Web.config
    Web.dev.config
    Web.int.config
    Web.prod.config

但是,我们有许多测试应用程序可用来作为控制台应用程序来访问我们的Web服务.我们希望能够在这些配置文件上执行相同类型的转换,以便减少拾起构建体时与配置文件的手动编辑相关的测试工作.

However, we have a number of test applications that we use to hit our web services, implemented as Console applications. We'd like to be able to do the same type of transforms on these config files, so that we can reduce manual labor of testing related to manual editing of config files when picking up a build drop.

本质上,我们想要这样做:

Essentially, we want this:

App.config
    App.dev.config
    App.int.config
    App.prod.config

我对这些配置转换的理解是,它们与我们的Web项目中的相应发布配置文件有关:

My understanding of these config transforms is that they're related to the corresponding publish profiles in our web projects:

Properties
    PublishProfiles
        dev.pubxml
        int.pubxml
        prod.pubxml

我试图将类似的文件添加到我们的Console应用程序项目中,但是我怀疑这是Web发布MSBuild目标中实际上利用它们的东西.

I've attempted to add similar files to our Console app projects, but I suspect it's something in the web publishing MSBuild targets that actually utilizes them.

是否有一种方法可以专门挂钩到构建的这一部分,以便我们可以转换非Web配置?

Is there a way to hook into this part of the build specifically, so that we can transform non-web configs?

推荐答案

要将多个app.config添加到控制台应用程序:

To add multiple app.configs to a console application:

我正在使用Visual Studio2013.在此版本中,我们没有配置转换.请安装配置转换扩展.

I am using visual studio 2013. In this version we don't have a config transformation. Please install the configuration transformation extension.

首先在解决方案配置中添加环境.右键单击App.config文件.单击添加配置转换.它增加了多达解决方案配置.

First add environments in solution configuration. Right click on the App.config file. Click on the Add config Transform. It adds as many as solution configurations.

App.config中:

<appSettings>
    <add key="key" value="some value" />   
</appSettings>

将此代码复制到App.Release.Config:

<appSettings>
    <add key="same key which you have given in app.config" value="some new value" xdt:Transform="Replace" xdt:Locator="Match(key)" />   
 </appSettings>

最后,构建解决方案.

这篇关于用于C#控制台应用程序的App.config文件的Web.config样式转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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