2

I'm looking for a way to have a GUID in my web.config file, which gets transformed into a new GUID at publish-time.

For example:

<add key="someGuid" value="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" />

and when you publish the app using MSBuild or Visual Studio's publish screen, the resulting config file, after transformation, contains a brand-new GUID, like:

<add key="someGuid" value="ab15c61ba16d1651a6c89a621d64c4f8" />

Basically, I need a new GUID every time I publish the app, but also need the ability to replace this GUID on demand without a full recompile.

Before I build something custom, using a placeholder and replacing the text manually, I wanted to see if there's a config transform or built-in MSBuild function that could help.

2
  • Have you found a way to do it with xdt transforms in web.config.release? Apr 14, 2016 at 9:54
  • 1
    @Rumpelstinsk I ended up with something custom outside of web.config which actually worked better anyway. I don't know if there's a built-in way, and since this question was asked, they've done a ton of work with the framework, so I don't know if there's a simple built-in way, aside from the listed answer.
    – Joe Enos
    Apr 14, 2016 at 13:47

1 Answer 1

2

In MSBuild, you can create a new GUID like this

<PropertyGroup>
  <SomeGuid>$([System.Guid]::NewGuid())</SomeGuid>
</PropertyGroup>

And then you can use the XmlPoke task to make the replacement using your newly created property $(SomeGuid).

Excerpted from in the book MSBuild Trickery tricks #9 and #101

1
  • For those of us, who haven´t purchased your book, could you please provide an example of how to make it work using the XmlPoke task without altering the web.config file to be transformed?
    – Matze
    Jun 10, 2022 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.