Can I insert elements into web.config with Web Deploy?
Asked Answered
L

4

9

Is it possible to insert an XML element into my web.config using Web Deploy's Parameters.xml system?

The XmlFile parameter "kind" appears to be the closest to what I need, but its match attribute only accepts an XPath query, and I don't appear to be able to specify a non-existent element in my XPath query. (Or rather, I can specify a non-existent element - Web Deploy just ignores it.) Specifically, I would like to transform this:

<configuration>
   <plugins>
      <add name="bleh"/>
   </plugins>
</configuration>

into this:

<configuration>
   <plugins>
      <add name="bleh">
        <option name="foo" value="bar"/>
      </add>
   </plugins>
</configuration>

(Unfortunately I can't pre-stock the web.config with an empty option element because this particular plugin system doesn't like unrecognized/empty options.)

Thanks for any ideas!

Lucilla answered 19/4, 2011 at 16:41 Comment(1)
Good question, +1. See my answer for a complete, short, simple and easy solution.Sclerophyll
Y
3

Such things are now possible starting with the Web Deploy V3. See the official documentation.

Here is one example of a parameters.xml file which will add newNode to all nodes including the root in target xml file:

<parameters>
  <parameter name="Additive" description="Add a node" defaultValue="&lt;newNode />" tags="">
    <parameterEntry kind="XmlFile" scope=".*" match="//*" />
  </parameter>
</parameters>
Yeaton answered 27/1, 2016 at 12:29 Comment(2)
Their solution is to add encoded XML blocks as values. I really find this approach ridiculous. What makes it worse is that it's the "official" solution from Microsoft. I have some fairly complex XML sections that need to be added at deployment time from TFS into Azure, and this is just horrible to work with.Asafetida
Consider this as a hack rather than a solution. I agree this approach is horrible for complex blocks. Could you use web.config transforms instead?Yeaton
S
1

Xpath is only a query language for XML documents -- it cannot by itself change an XML document or create a new XML document.

The language especially designed for transforming an XML document is called XSLT.

Here is a very short and simple XSLT transformation that solves your problem:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="add[@name='bleh']">
  <xsl:copy>
   <xsl:copy-of select="@*"/>
   <option name="foo" value="bar"/>
  </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the provided XML document:

<configuration>
    <plugins>
        <add name="bleh"/>
    </plugins>
</configuration>

the wanted, correct result is produced:

<configuration>
   <plugins>
      <add name="bleh">
         <option name="foo" value="bar"/>
      </add>
   </plugins>
</configuration>
Sclerophyll answered 26/4, 2011 at 13:38 Comment(3)
XSLT would be great - and thanks for the example! - but how can I integrate such a transformation into the Web Deploy process?Lucilla
@ladenedge: I am not familiar with Web Deploy, but if it allows using any programming language (such as C#, VB.NET, JScript) or even a command-line program, then one can initiate the transformation in a standard way.Sclerophyll
There is a runCommand provider for Web Deploy, though I'm running into a problem of relative paths. Anyway, an external XSLT does seem to be the right answer here, so thank you for your help!Lucilla
S
1

Have you considered using configSource? This allows you to split up your configuration files into multiple smaller files. The web.config would go from:

<configuration>
   <plugins>
      <add name="bleh"/>
   </plugins>
</configuration>

To this with configSource:

<configuration>
   <plugins configSource="plugins.config"/>
</configuration>

The downside is You won't get a nice UI for editing config values during deployment. It is something to consider if you cannot get parameterization to work. If the install UI is what you are after then you could code an editing tool for your admins that can create and modify the plugin.xml files..

Schnell answered 1/5, 2011 at 21:51 Comment(0)
B
0

Can you just use the regular web.config transformation practices? http://blogs.msdn.com/b/webdevtools/archive/2009/05/04/web-deployment-web-config-transformation.aspx

You can do the code below which will replace all of your section with the one below. Actually, what you have in your questions should replace your entire section with the one you provided if it was your Web.Release.config file.

Web.Release.config:

<plugins>
  <add name="bleh">
     <option name="foo" value="bar"/>
  </add>
</plugins>
Banjermasin answered 19/4, 2011 at 19:30 Comment(1)
Thanks for the comment! Unfortunately I need to do this transformation at deployment time, not at build time. That is, I need to add option depending on which plugin one chooses to deploy.Lucilla

© 2022 - 2024 — McMap. All rights reserved.