【问题标题】:App.config transforms aren't applied to AppName.exe.configApp.config 转换不适用于 AppName.exe.config
【发布时间】:2016-01-07 02:58:21
【问题描述】:

我的 WPF 项目中有一个 app.config 文件,它使用 .csproj 中的以下构建目标进行了转换

<!-- MSbuild Task for transforming app.config based on the configuration settings -->
<UsingTask TaskName="TransformXml" 
           AssemblyFile="$(SolutionDir)\packages\MSBuild.Microsoft.VisualStudio.Web.targets.12.0.4\tools\VSToolsPath\Web\Microsoft.Web.Publishing.Tasks.dll" />
<Target Name="AfterBuild">
  <!-- Transform the app.config into the correct config file associated to the current build settings -->
  <TransformXml Source="App.config" Transform="App.$(Configuration).config" Destination="$(OutputPath)\App.config" />
  <!-- Bit of .Net to get all folders and subfolders that we want to delete post-build -->
  <ItemGroup>
    <FoldersToClean Include="$([System.IO.Directory]::GetDirectories(&quot;$(OutputPath)&quot;))" />
  </ItemGroup>
  <!-- Delete all of our localization folders and .pdb symbols. We only delete PDB files for Release builds. Debug builds we will leave them. -->
  <RemoveDir Directories="@(FoldersToClean)" />
  <Exec Command="del $(OutputPath)*.pdb" Condition=" '$(Configuration)'=='Release' " />
</Target>

这会在编译后正确生成 App.config,并创建转换后的 QA 或生产配置文件。然而,我遇到的问题是以下代码使用了 AppName.exe.config。

string encryptedString = ConfigurationManager.AppSettings["SqlConnection"];

在阅读了一些内容之后,我明白了为什么会这样。 App.config 文件最终成为 AppName.exe.config 以供在运行时使用。没关系;不过,我的转变发生得很晚。编译完成后,AppName.exe.config 文件包含我的基本 App.config 文件信息,并且没有任何转换后的设置。我认为这是由于转换发生在构建后步骤中,它在使用原始 App.config 文件生成 AppName.exe.config 之后转换 App.config。

看来没有什么能阻止我改变

<TransformXml Source="App.config" 
              Transform="App.$(Configuration).config" 
              Destination="$(OutputPath)\App.config" />

以便它在构建后替换 AppName.exe.config 文件。

<TransformXml Source="App.config" 
              Transform="App.$(Configuration).config" 
              Destination="$(OutputPath)\AppName.exe.config" />

这有什么问题吗?将 app.config 和转换用于桌面应用程序时,没有太多帮助。我在网上阅读的所有内容都是为了将​​内容转换为 web-config 文件。我认为这是安全的,或多或少是一回事。但是,我想确保通过将原始 AppName.exe.config 替换为转换后的 App.config 来确保不会遗漏任何明显的副作用或问题。

【问题讨论】:

    标签: wpf msbuild transformation


    【解决方案1】:

    安装此https://github.com/acottais/msbuild.xdt NuGet 包,您将能够创建类似于 web.config 的转换。

    还有其他几个 Nuget 包可以像 web.config 一样转换 app.config。

    前几天我用过这个,效果很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-26
      • 2019-09-02
      • 1970-01-01
      • 2018-06-19
      • 2022-11-24
      • 1970-01-01
      • 1970-01-01
      • 2014-07-24
      相关资源
      最近更新 更多