如何在发布构建期间用 configSource 替换连接字符串文件?

How to substitute configSource for connection strings file during relase build?

我想使用 Azure tfs 为 .net web 应用程序开发管道。我使用了已经为此类部署提供的 IIS Web 部署 template/utility。在发布管道中,我正在使用 XML 变量转换。但我面临的一个问题是,在项目中,我有多个 .config 文件,我已将它们包含在我的 web.config 文件中,如下所示

<configuration>
  <connectionStrings configSource="None"/>  
  
  <appSettings file="None">
  </appSettings>

根据不同环境的发布,我在发布过程中进行了变量替换。例如,文件夹 app_data 包含 app_sttings_*.config 个文件,文件变量会被替换。但是我的连接字符串文件有问题,我无法在发布期间更改 configSource

我是做错了还是漏掉了一步?

因为 configSource 属性不能被 IIS Web App Deploy 任务替换。可以使用扩展工具Magic Chunks替换configSource值

您可以在您的 azure devops 组织中安装 Magic Chunks 扩展。并在 IIS Web App Deploy 任务 之前添加 Config transformation 任务以替换 configSource 值。

首先,您需要定义一个变量(例如 configSource)以在 Release Pipeline 变量部分保存 configSource 值。然后配置 Transformations 部分 Config transformation 任务如下

{
  "configuration/connectionStrings/@configSource": "$(configSource)"
 }  

还有另一个 替换令牌 任务可以替换 configSource 值。你可以看看here.

它在 IIS Web App Deploy task 中表示,发布管道中定义的变量将与“key”或“name[=35”相匹配=]' 配置文件的 appSettings、applicationSettings 和 connectionStrings 部分中的条目。也许这就是 configSource 不能被 IIS Web App Deploy 任务替换的原因。

有关详细信息,请参阅 File transforms and variable substitution reference