skip to Main Content

I have an old .NET project. I created an Azure Devops pipeline for it, a build and a deployment pipeline and I’ll deploy it in 2 different environments.

I store some secret in Azure Keyvault, and the remaining parameters in Variable group.

Everything works perfectly, except for one thing:
It’s an old project, so I have a Web.config file and during release, I want to replace the environment-specific values, such as the connectionString or ApiKey.

I am able to successfully replace and change everything under the appSettings , but I am unable to replace the connectionString value under the connectionString.

Web.config file:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  https://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
   <connectionStrings>
        <add name="MyDB"  connectionString="Standard Server=$(Server);Database=$(Database);User ID=$(User);Password=$(Password);Trusted_Connection=False;Encrypt=True;"/>
    </connectionStrings>
  <appSettings>
    <add key="ApiKeyGeoCoding" value="$(ApiKeyGeoCoding)" />
    <add key="ApiKeyTimeZone" value="$(ApiKeyTimeZone)" />
  </appSettings>

deployment.yml

                - task: AzureKeyVault@2
                  inputs:
                    azureSubscription: ${{ parameters.azureServiceConnection }}
                    KeyVaultName: ${{ parameters.keyvaultName }}
                    SecretsFilter: "*"
                    RunAsPreJob: false
                - task: IISWebAppManagementOnMachineGroup@0
                  inputs:
                    IISDeploymentType: IISWebsite
                    ActionIISWebsite: StopWebsite
                    StartStopWebsiteName: ${{ parameters.IIS_SiteName }}
                - task: IISWebAppDeploymentOnMachineGroup@0
                  inputs:
                    WebSiteName: ${{ parameters.IIS_SiteName }}
                    Package: $(Pipeline.Workspace)/**/${{ parameters.packageName }}
                    XmlVariableSubstitution: true

I have already tried using the FileTransform task and it did not work as expected.

How can I resolve this issue and make sure that the correct values are placed in my Web.config file?

2

Answers


  1. The 3rd party Replace Tokens task should be able to do the job well. It is similar to the FileTransform task but is hugely customisable and works with any text files.

    In your example, with the default Replace Tokens token pattern prefix and suffix #{}# your Web.Config would look like this:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
       <connectionStrings>
            <add name="MyDB" connectionString="Standard Server=#{Server}#;Database=#{Database}#;User ID=#{User}#;Password=#{Password}#;Trusted_Connection=False;Encrypt=True;"/>
        </connectionStrings>
      <appSettings>
        <add key="ApiKeyGeoCoding" value="#{ApiKeyGeoCoding}#" />
        <add key="ApiKeyTimeZone" value="#{ApiKeyTimeZone}#" />
      </appSettings>
    

    This will replace the tokens in your file with the variable values from you pipeline. The Replace Tokens task has many very useful options, and you can also change the token pattern to suit.

    I recommend taking some time to look at each parameter of the task.
    I personally like to set actionOnMissing: fail. This has helped me to avoid deploying things when I’ve defined the token in the config file, but forgotten to add the variable to the pipeline. Without this, the replacement would change the value to an empty string.

    Login or Signup to reply.
  2. Did you try to add the property in the .csproj as mentioned here https://devblogs.microsoft.com/dotnet/asp-net-web-application-publishpackage-tokenizing-parameters/#prevent-tokenizing-connection-strings ?

    <PropertyGroup>    
    <AutoParameterizationWebConfigConnectionStrings>false</AutoParameterizationWebConfigConnectionStrings>
    </PropertyGroup>
    

    Also look into adding new parameters in your arguments – in case you did not.
    /p:TransformWebConfigEnabled=false
    /p:AutoParameterizationWebConfigConnectionStrings=false

    Also see this:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search