1

We're using a library that needs to have a value updated in the web.config with every build. Is there a way to do this natively?

We're using a library that has a custom config section in the web.config

The version attribute needs to be updated with every build. Increment or even DateTime.Now equivalent would work.

<clientDependency version="6">
...
</clientDependency>
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
user3953989
  • 1,844
  • 3
  • 25
  • 56
  • Can you elobrate on what needs to be updated and why? This seams not beeing best practice. However using the new Task Runner Explorer and the [config "include" feature](https://stackoverflow.com/questions/10300063/can-i-create-config-file-and-include-it-to-web-config) it might be possible. – Christian Gollhardt Jul 19 '17 at 13:06
  • You can use web config transforms for things like different connection string or credentials for dev / test / prod, but if it's something like a build # that changes every build, you may need a post-build task that manipulates the config file. – Glenn Ferrie Jul 19 '17 at 13:07
  • 1
    If you meant change on *deploy*, instead of build, you can do transformations. Check this: [How to: Transform Web.config When Deploying a Web Application Project](https://msdn.microsoft.com/en-us/library/dd465318(v=vs.100).aspx) – Anderson Pimentel Jul 19 '17 at 13:08
  • @ChristianGollhardt I added more detail to the question but yeah doesn't seem like it should have been implemented this way – user3953989 Jul 19 '17 at 13:12
  • @GlennFerrie & Anderson Pimentel It needs to be per build so it will update while we are doing dev and not have to publish everytime we want to see an update locally. Also, the number has to be dynamic so transforms won't work to my knowledge. I updated the OP with more detail – user3953989 Jul 19 '17 at 13:13
  • 1
    Possible duplicate of [Make Web.config transformations working locally](https://stackoverflow.com/questions/3613714/make-web-config-transformations-working-locally) – Panagiotis Kanavos Jul 19 '17 at 13:17
  • @user3953989 you can apply transformations during build with the TransformXML build task – Panagiotis Kanavos Jul 19 '17 at 13:17

3 Answers3

4

You can try using MSBuild Tasks.

Maybe XMLPoke to update the web.config with a formatted date/time string:

<Target Name="EchoTime">
    <Time Format="yyyyMMddHHmmss">
        <Output TaskParameter="FormattedTime" PropertyName="currentTime" />
    </Time>
    <Message Text = "$(currentTime)" />
</Target>

<Target Name="UpdateWebConfig">
  <XmlPoke 
      XmlInputPath="web.config"
      Query="//<complete-path>/clientDependency/@version"
      Value="$(currentTime)" />
</Target>

Moving the updatable section to an external file, using configSource could also make the job easier.

Anderson Pimentel
  • 5,086
  • 2
  • 32
  • 54
1

You should first review, why exactly the config change on every build. This seems not to be good. Maybe you can change this.

However, with Visual Studio 2013 the Gulp Task Runner was introduced.

You could abuse it a little bit.

Basicly:

Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111
  • 1
    First, this is neither unusual nor bad. There *are* different builds (Release vs Debug) and environments (Development vs Testing vs Staging vs Production). Second, this is already available in the *deployment* pipeline. You *can* apply different transformations per build *and* environment. There's no need to use external tools. – Panagiotis Kanavos Jul 19 '17 at 13:19
  • In fact, this functionality is supported not only by Visual Studio but deployment tools like Octopus Deploy, Release Management Services etc, in order to make deployment to different environments easier – Panagiotis Kanavos Jul 19 '17 at 13:20
  • @PanagiotisKanavos Can you provide an example of how to increment that value on each build? I know how to replace it with a static value using transforms – user3953989 Jul 19 '17 at 13:20
  • The same mechanism is also used in NuGet and Chocolatey to apply transformations to config files during deployment – Panagiotis Kanavos Jul 19 '17 at 13:21
  • @Christian Gollhardt I may have to end up doing this if nothing native is available. – user3953989 Jul 19 '17 at 13:21
  • @user3953989 not to my knowledge, if it must be realy on every build. But maybe somebody comes with a better solution – Christian Gollhardt Jul 19 '17 at 13:22
-1

Here is other sample to do with XmlPoke in MSBuild.

Auto replace VersionBuild value with yyyyMMddmmss when publish

Web.config
<appSettings>
    <add key="VersionBuild" value=".121"/>
</appSettins>
<Target Name="BeforeBuild">
    <PropertyGroup>
      <CurrentDate>$([System.DateTime]::Now.ToString(yyyyMMddmmss))</CurrentDate>
    </PropertyGroup>
    <XmlPoke 
      Condition="'$(PublishProfileName)' != ''"
      XmlInputPath="Web.config"
      Query="/configuration/appSettings/add[@key = 'VersionBuild']/@value"
      Value=".$(CurrentDate)" />
</Target>
hoangitk
  • 94
  • 5
  • 2
    If you wonder why the downvotes - the question that's been asked almost 2 years ago, your answer only contains a link that's already been mentioned. – trailmax Jul 18 '19 at 09:53