在appSettings外部更新web.config中的变量

时间:2019-11-07 16:15:42

标签: azure-devops

我需要在Azure TFS中更新web.config中的值。我能够获取appSettings中一部分connectionString的替换值(要使其正常工作,我需要启用名为XML变量替换的设置(在IIS Web Deploy下)。

但是,web.config中的其他区域无法替换。

我尝试了几种不同的方法,使用不同的任务替换令牌,使用变量的“发布”或“环境”设置,使用变量组。但是,这些都不起作用。

当前,我正在使用“替换令牌”任务(可从https://github.com/qetza/vsts-replacetokens-task#readme获得)

我已将令牌前缀和后缀设置为__(以与web.config匹配)

这是web.config文件的摘录

<?xml version="1.0"?>
<configuration>
    <appSettings>
        <add key="ConnectionString" value="__ConnectionString__"/>
    </appSettings>
    <system.web>
        <pages theme="__Theme__" controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID">
            <controls>
                <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
            </controls>
        </pages>
    </system.web>
    <system.serviceModel>       
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
        <bindings>
            <customBinding>
                <binding name="TestBinding1">
                    <textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16" messageVersion="Soap12" writeEncoding="utf-8">
                        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    </textMessageEncoding>
                    <httpsTransport manualAddressing="false" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" allowCookies="false" authenticationScheme="Anonymous" bypassProxyOnLocal="false" decompressionEnabled="true" hostNameComparisonMode="StrongWildcard" keepAliveEnabled="true" maxBufferSize="65536" proxyAuthenticationScheme="Anonymous" realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false" useDefaultWebProxy="true" requireClientCertificate="false" />
                </binding>
            </customBinding>
            __basicHttpBindingOptionalBinding__
        </bindings>
        <client>
            <endpoint address="__TestEndPoint__" binding="customBinding" bindingConfiguration="TestBinding1" contract="BSEInspectionsWebServiceForFDA.StateDataTransfer" name="StateDataTransferPort" />
            __endpointOptionalEndpoint__
        </client>
    </system.serviceModel>
</configuration>

我希望basicHttpBindingOptionalBinding和EndpointOptionalEndpoint仅空行。 主题需要用TestTheme代替,并且TestEndPoint需要使用变量中定义的值进行设置。

2 个答案:

答案 0 :(得分:1)

将parameter.xml添加到您的项目中,如下所述: https://docs.microsoft.com/en-us/aspnet/web-forms/overview/deployment/web-deployment-in-the-enterprise/configuring-parameters-for-web-package-deployment

在此处指定参数,例如“ myEndpointAddress”。

然后在IIS Web部署任务中使用其他参数传递值 -setParam:name ='myEndPointAddress',value ='new_value'

答案 1 :(得分:0)

要替换__Theme__, __TestEndPoint__,我建议扩展任务Magic Chunks

您可以将Magic Chunks任务安装到您的组织并将其添加到管道中。它使用以下用于魔术块任务的示例设置测试了您的web.config:enter image description here

要替换 basicHttpBindingOptionalBinding ,我建议另一个扩展任务RegEx Find & Replace

enter image description here

您可以在两个任务中引用管道变量。我在测试管道中添加了上述两项任务。 web.config中的值已成功替换。

更新:要使用powershell任务从运行脚本的以下构建工件中提取web.config:

您需要自己配置$sourceFile$destFile$sourceFile应该是zip文件的绝对路径。

$sourceFile="$(Build.ArtifactStagingDirectory)\AboutSite.zip"
$destFile="$(Build.BinariesDirectory)\web.config"

Add-Type -Assembly System.IO.Compression.FileSystem
$zip = [IO.Compression.ZipFile]::OpenRead($sourceFile)
$zip.Entries | where {$_.Name -like 'web.config'} | foreach {[System.IO.Compression.ZipFileExtensions]::ExtractToFile($_, $destFile, $true)}
$zip.Dispose()