加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 为3种不同的环境转换app.config

发布时间:2020-12-15 17:44:36 所属栏目:百科 来源:网络整理
导读:我需要能够使用msbuild转换我的app.config文件.我可以转换文件,如果它被称为app.DEBUG.config或app.Release.config,但我不能添加一个名为app.PROD.config的文件. 使用常规XDT转换如果我选择不同的PublishProfile,msbuild会识别不同的web.config文件 msbuild
我需要能够使用msbuild转换我的app.config文件.我可以转换文件,如果它被称为app.DEBUG.config或app.Release.config,但我不能添加一个名为app.PROD.config的文件.

使用常规XDT转换如果我选择不同的PublishProfile,msbuild会识别不同的web.config文件

msbuild path.to.project.csproj Configuration=Release PublishProfile=DEV

显然app.config不能使用相同的设置.我总是可以为DEV.config设置创建一个特定的构建配置,但对一个应用程序进行单独的构建确认似乎没用.一个hacky方法是只为每个环境POST-BUILD复制正确的app.config.

我试图使用SlowCheetah插件,但这似乎只是转换默认的DEBUG和RELEASE配置文件,这是不合适的,因为我有两个以上的环境.如果我确实使用了这个错误,请让我知道我应该将哪个参数传递给msbuild来选择我的app.DEV.config.

预期的结果是msbuild将根据自定义的变换app.DEV.config或为app.PROD.config自定义的变换来变换app.config.我希望有一个参数可以传递给msbuild,这将允许我使用Release配置,但每个环境使用不同名称的转换.

解决方法

这是我在这种情况下使用的:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <!-- This target will run right before you run your app in Visual Studio -->
  <Target Name="UpdateWebConfigBeforeRun" BeforeTargets="Build">
    <Message Text="Configuration: $(Configuration) update from web.template.$(Configuration).config"/>
    <TransformXml Source="web.template.config"
              Transform="web.template.$(Configuration).config"
              Destination="web.config" />
    </Target>

  <!-- Exclude the config template files from the created package -->
  <Target Name="ExcludeCustomConfigTransformFiles" BeforeTargets="ExcludeFilesFromPackage">
    <ItemGroup>
      <ExcludeFromPackageFiles Include="web.template.config;web.template.*.config"/>
    </ItemGroup>
    <Message Text="ExcludeFromPackageFiles: @(ExcludeFromPackageFiles)" Importance="high"/>
  </Target>
</Project>

我有以下设置:

web.template.config
    - web.template.debug.config
    - web.template.production.config
    - web.template.release.config etc

应该在不需要额外插件的情况下工作.在你的场景中,你需要编辑内容来说app.而不是网络.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读