How to force an element to be replaced using configuration conversion in Visual Studio?

We know that Visual Studio has this great feature for converting Web.config to publishing. So we can have something like the following in Web.Release.config to replace the original value in Web.config:

Web.config:

<add key="SomeKey" value="DebugValue"/> 

Web.Release.config:

 <add key="SomeKey" value="ReleaseValue" xdt:Transform="Replace" xdt:Locator="Match(key)"/> 

All this is good if you do not have typos in the conversion configuration. So, for example, if your conversion configuration has the following line:

 <add key="SomeKeyTypo" value="ReleaseValue" xdt:Transform="Replace" xdt:Locator="Match(key)"/> 

Then the β€œSomeKey” element will not be replaced because the conversion will not find the key and will quietly print this warning:

 ...\Web.Release.config: Warning : No element in the source document matches '/configuration/appSettings/add[@key='SomeKeyTypo']' 

Problem is that in some situations, for example, if you change the key in the main Web.config, and you forget to change it in Web.Release.config when you publish, there is a good chance that you do not read the output messages of the publication, and you skip the warning, which may end in disaster, because the Debug value will be used in your application instead of the Release value.

So the question is is when you define a conversion in Web.Release.config, is there a way to indicate that this element MUST be replaced, and if the key is not found throw and ERROR instead of WARNING and basically exit the publication with some error code?

+4
source share
1 answer

You can set the Visual Studio parameter to "Handle warnings as errors", which then leads to an error that impedes the publication task. You can even configure it to treat only this particular warning as an error.

You can find the dialog box by going to the property page of the project you want to deploy, and then in the "Assembly" section below you will find your options.

0
source

All Articles