2

I am lost. I've got a Web project that comes with a config file and several parameters which need to be set differently per target environment. I have an app setting and a connection string. The DEFINE values need to be replaced.

Web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <appSettings>
        <add key="HostUri" value="DEFINE"/>
        ...
    </appSettings>
    <connectionStrings>
        <add name="DbConn" connectionString="DEFINE" providerName="System.Data.SqlClient"/>
    </connectionStrings>
    ...
</configuration>

I've got a parameter specification which an Azure App Service Deploy task uses later during deployment. Here, I specified both values to be replaced.

parameters.xml:

<parameters>
    <parameter name="HostUri" defaultValue="Replace me!">
    <parameterEntry kind="XmlFile" scope="Web.config"
        match="/configuration/appSettings/add[@key='HostUri']/@value" />
</parameter>

<parameter name="DbConn" defaultValue="Replace me!">
    <parameterEntry kind="XmlFile" scope="Web.config"
        match="/configuration/connectionStrings/add[@name='DbConn']/@connectionString" />
  </parameter>
</parameters>

While the app setting HostUri is correctly replaced, the connection string DbConn stays untouched. I do not even get the default value set. It keeps its value DEFINE as originally set in the Web.config.

My gut tells me my XPath /configuration/connectionStrings/add[@name='DbConn']/@connectionString is incorrect, but it looks okay to me and follows what other posts have done, such as Cobus Bernard.

Any help is greatly appreciated.

Quality Catalyst
  • 6,531
  • 8
  • 38
  • 62

1 Answers1

2

I studied log files and found that MSBuild treats connection strings differently:

...
Adding declared parameter 'HostUri'.
Adding declared parameter 'DbConn'.
Adding declared parameter 'DbConn-Web.config Connection String'.
...

The last line led to an unwanted entry in the the generated file SetParameters.xml:

...
<setParameter name="HostUri" value="Replace me!" />
<setParameter name="DbConn" value="Replace me!" />
<setParameter name="DbConn-Web.config Connection String" value="DEFINE" />
...

So my XPath was correct, but this special directive put a DEFINE into my target Web.config. I found two solutions to this, primarily based on this SO question: How to Publish Web with msbuild?

1) Tweak your project file

Unload you Web project, edit the project file and add in your desired configuration the following line:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <!-- Add this line: --
    <AutoParameterizationWebConfigConnectionStrings>False</AutoParameterizationWebConfigConnectionStrings>
    ...
</PropertyGroup>

The build process will pick this up and stops treating connection strings special.

I find this solution too hidden and people will forget so the alternative is a bit more explicit:

2) Configure MSBuild

Pass an explicit argument to MSBuild when the release package gets built:

msbuild /P:Configuration=Release;AutoParameterizationWebConfigConnectionStrings=false;...
Community
  • 1
  • 1
Quality Catalyst
  • 6,531
  • 8
  • 38
  • 62