Saturday 3 November 2012

Configuration file XML value substitution and other operations in Wix

I've been meaning to write this post for a while now, finally today I got around it.

I've used various types of XML operations, the list is by no means comprehensive, it's a just a list of operations that I have had the need to use.

Remember that if you need to set more than one value then you need to set the action to bulksetValue. This might be useful for custom configurations where you might need to set the same value for multiple attributes.

Change SQL Server Connection String
XML:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <connectionStrings>
   <add name="loggingdb"
    connectionString="Data Source=devserver;Initial Catalog=Logging;Integrated Security=SSPI;"
    providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>
Wix Snippet:
<util:XmlFile Id="ConnectionString"  File="[WEBDIR]web.config" Action="setValue" 
ElementPath="/configuration/connectionStrings/add[\[]@name='Logging'[\]]/@connectionString"
Value="Data Source=[LOGGINGDB];Initial Catalog=Logging;Integrated Security=SSPI;" Permanent="yes"/>
where WEBDIR is the directory path of the website and LOGGINGDB is the database instance.

Change AppSettings Value
XML:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 <appSettings>
  <add key="serviceurl" value="http://devserver/service.svc" />
 </appSettings>
</configuration>
Wix Snippet:
<util:XmlFile Id="service"  File="[WEBDIR]web.config" Action="setValue"
 ElementPath="/configuration/appSettings/add[\[]@key='serviceurl'[\]]/@value"
 Value="[URL]" Permanent="yes"  />
where WEBDIR is the directory path of the website and URL is the service url.

Change Value
XML:
<?xml version="1.0" encoding="utf-8"?>
<entities>
  <pre_details>
    <pre_name>MyORG</pre_name>
    <pre_url>http://devserver/myservice.svc</pre_address>
  </pre_details>
</entities>
Wix Snippet
<util:XmlFile Id="admin"  File="[WEBDIR]Reference Data\settings.xml" Action="setValue"
 ElementPath="entities/pre_details/new_name[\[]text()='http://uk13565/myservice.svc'[\]]"
 Value="http://[HOSTNAME]/myservice.svc" Permanent="yes"  />
where WEBDIR is the directory path of the website and and HOSTNAME is the web server hostname.

Change WCF Endpoint Address
XML:
<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:30:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
            <message clientCredentialType="UserName" algorithmSuite="Default"/>
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://devserver/Service.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService" contract="IService" name="BasicHttpBinding_IService"/>
    </client>
  </system.serviceModel>
Wix Snippet:
 <util:XmlFile Id="config"  File="[WEBDIR]web.config" Action="setValue"
  ElementPath="/configuration/system.serviceModel/client/endpoint[\[]@address='http://uk1212/Service.svc'[\]]/@address"
  Value="http://[HOSTNAME]/Service.svc" Permanent="yes"  />
where WEBDIR is the directory path of the website and HOSTNAME is the web server hostname.

Change Settings Value
XML:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <section name="Processor.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
        </sectionGroup>
    </configSections>
    <applicationSettings>
        <Processor.Properties.Settings>
            <setting name="StartTime" serializeAs="String">
                <value>21:00</value>
            </setting>
        </Processor.Properties.Settings>
    </applicationSettings>
</configuration>
Wix Snippet:
 <util:XmlFile Id="SASA"  File="[PROCESSOR]Processor.exe.config" Action="setValue"
  ElementPath="/configuration/applicationSettings/Processor.Properties.Settings/setting[\[]@name='StartTime'[\]]/value"
  Value="22:00" Permanent="yes"  />
where PROCESSOR is the directory path of the windows service.

Hope it helps.

1 comment:

  1. This will not work if you add a second 'setting' element below the existing StartTime setting. I couldn't figure out a way around this with XmlFile, however with XmlConfig it worked.

    ReplyDelete