WiX Wednesday 1.3 Update Config Files During Deployment

Intro

There are many ways to run a find and replace in a config file to environmentalize it: you can update files in the build before they are compiled into the MSI, and even here there are multiple choices: SlowCheetah being one of them. This only works if you know at runtime the servers you are deploying to, or you only want a direct 1 to 1 connection. So on those occasions that you want to deploy to more than one box, updating at runtime does not work. Fortunately there is a way to update using WiX, and is surprisingly straightforward.

Your WiX project will need a reference to the WixUtilExtension dll. Within the Product.wxs file, within the “Feature” key value pair I added a ComponentRef and gave it the Id of “XmlUpdateConfig”.


<Feature Id="ProductFeature" Title="Red.Phoenix" Level="1">
<ComponentRef Id="XmlUpdateConfig"/>
</Feature>

I added a separate wxs file to the installer project.Below I will discuss the values you have to enter:


<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"
xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">
<Fragment>
<DirectoryRef Id="WebBin">
<Component Id="XmlUpdateConfig"
Guid="{5AEF24F2-022C-4B19-8F44-91663AC2EB62}" >
<CreateFolder />
<util:XmlFile Id="DataServiceCommand"
Action="setValue"
ElementPath="//system.serviceModel/client/endpoint[\[]@name='tcpMarketService'[\]]/@address"
Value="net.tcp://[MARKETDATASVCLOCATION]:[MARKETDATATCPPORT]/Services/data/DataServiceCommand.svc"
File="[INSTALLLOCATION]Website\web.config"
SelectionLanguage="XPath"
Sequence="1" />
<util:XmlFile Id="DataServiceQuery"
Action="setValue"
ElementPath="//system.serviceModel/client/endpoint[\[]@name='tcpWorkingService'[\]]/@address"
Value="net.tcp://[MARKETDATASVCLOCATION]:[MARKETDATATCPPORT]/Services/data/MarketDataQuery.svc"
File="[INSTALLLOCATION]Website\web.config"
SelectionLanguage="XPath"
Sequence="1" />
</Component>
</DirectoryRef>
</Fragment>
</Wix>

The two endpoint addresses I am editing look like this:

<pre><client>
<endpoint address="net.tcp://localhost:809/Services/data/DataServiceCommand.svc" binding="netTcpBinding" bindingConfiguration="MarketTcp" contract="ServiceFacade.Interfaces.Service.MarketService" name="tcpMarketService" behaviorConfiguration="largeMessage" />
<endpoint address="net.tcp://localhost:809/Services/data/MarketDataQuery.svc" binding="netTcpBinding" contract="ServiceFacade.Interfaces.Service.DayService" name="tcpWorkingService" behaviorConfiguration="largeMessage" />
</client>
  • For DirectoryRef I am using the Id of the directory that the file will be deployed to as this will be completed after the InstallFiles task. I believe that this is implicit within WiX as I have not had to configure this like I would for a CustomAction.
  • The ComponentId is the same as the ComponentRefId in the product.wxs file.
  • For each value I want to update  I have to give a unique name otherwise the installer will fail.
  • For each action I want I use the SetValue. SetValue sets the text value inside the node specified in ElementPath.
  • The Element path is an xpath to the value I want updated.
  • The Value is what I want updated. The values within square brackets are variables that I pass in when i run the “msiexec /i” command. So lt’s say this MSI was called WebService, the msiexec command would look like this:WixWednesday1_3_1
  • File pertains to the file location of the config file.
  • Other than “setvalue” it is also possible to edit the files in other ways using xpath:

WixWednesday1_3

Summary

Wow, something in WiX that is quick and simple and works well! Facetiousness aside, it’s very straightforward to update files on deployment and the more i think about it, the more sense it makes to use this method over any other method as it makes the MSI’s environment agnostic.

Sources

  1. Download WiX from CodePlex here.
  2. As I said, I don’t think the manual for WiX is all that great. It feels like it’s been written by an expert in WiX for an expert in WiX. But I guess at least there is a manual.
  3. There’s also a very comprehensive tutorial written about WiX that explains it in far greater detail than I have here. It also provides plenty of examples. Such as in the advanced sections there is a tutorial on creating your own custom UI.
  4. I’ve probably short-changed msiexec. Aside from running msiexec /? in the cmdline, there’s plenty of examples on the TechNet site.
  5. There’s even a book available! I’ve not read it, however the reviews on amazon.com seem positive. For such an esoteric piece of software, and at a reasonable price it’s worth a go? Maybe I’ll buy it and review it at the end of this series….

Author: Richie Lee

Full time computer guy, part time runner. Full time Dad, part time blogger. Knows a thing or two about Pokémon. Knows too much about SQL Agent. Writer of fractured sentences. Maker of the best damn macaroni cheese you've ever tasted.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s