Showing posts with label Wix. Show all posts
Showing posts with label Wix. Show all posts

Friday, September 13, 2013

Create Wix installer for Windows Service

In this article I'm going to discuss create wix installer for windows service. in this scenario i considered about following situation.
  • Attach Exe with App.config
  • Attach external dll
  • Modify App.config values based on installed location.
for this you can use either visual studio editor or Wix Editor. 
 <?xml version="1.0" encoding="utf-8"?>  
 <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">  
   <Product Id="5AC767F2-4F21-45DF-9DAB-C013C62B2B67" Name="Installer" Language="1033" Version="1.0.0.0" Manufacturer="TRT " UpgradeCode="3fd83540-23f6-45f4-bb1c-e77c12725680">  
     <Package InstallerVersion="200" Compressed="yes" />  
     <Media Id="1" Cabinet="SampleApp.cab" EmbedCab="yes" />  
     <Directory Id="TARGETDIR" Name="SourceDir">  
       <Directory Id="ProgramFilesFolder">  
         <Directory Id="INSTALLFOLDER" Name="Installer">  
           <Directory Id="TestService" Name="TestService">  
             <Component Id="MainExecutable" Guid="CE30D5D8-3211-4890-A9DC-0C315C15B39E">  
               <File Id="testServiceexe" Name="TestInstallerProject.exe" DiskId="1" KeyPath="yes" Source="..\TestInstallerProject\bin\Debug\TestInstallerProject.exe" />  
               <File Id="TestInstallerProject.exe.config" Name="TestInstallerProject.exe.config" Source="..\TestInstallerProject\App.config" Vital="yes" DiskId="1" />  
               <File Id="GOOGLE.GDATA.ANALYTICS.DLL" Name="Google.GData.Analytics.dll" Source="..\TestInstallerProject\bin\Debug\Google.GData.Analytics.dll" />  
               <File Id="GOOGLE.GDATA.CLIENT.DLL" Name="Google.GData.Client.dll" Source="..\TestInstallerProject\bin\Debug\Google.GData.Client.dll" />  
               <ServiceInstall Name="ScheduledService" Type="ownProcess" Start="auto" ErrorControl="ignore" Id="ServiceInstaller" DisplayName="ScheduledService" Account="[SERVICEACCOUNT]" Password="[SERVICEACCOUNTPASSWORD]">  
                 <util:ServiceConfig FirstFailureActionType="restart" SecondFailureActionType="restart" ThirdFailureActionType="restart" ResetPeriodInDays="1" RestartServiceDelayInSeconds="1" />  
               </ServiceInstall>  
               <ServiceControl Id="StartService" Name="ScheduledService" Start="install" Stop="both" Remove="uninstall" Wait="no" />  
               <RemoveFolder Id="INSTALLDIR" On="uninstall" />  
               <util:XmlFile Id="ModifyServiceLocation" ElementPath="//configuration/appSettings/add[\[]@key='TextLocation'[\]]/@value" File="[TestService]\TestInstallerProject.exe.config" Action="setValue" Value="[TestService]ScheduledServiceNew.txt" PreserveModifiedDate="yes" SelectionLanguage="XPath" Sequence="1" />  
             </Component>  
           </Directory>  
         </Directory>  
       </Directory>  
     </Directory>  
     <Feature Id="Complete" Title="Installer" Level="1">  
       <ComponentRef Id="MainExecutable" />  
     </Feature>  
     <UI>  
     </UI>  
   </Product>  
 </Wix>  

when attache app.config, you have to consider about name and the id of the config. it should be the name of the exe.

 <File Id="TestInstallerProject.exe.config" Name="TestInstallerProject.exe.config" Source="..\TestInstallerProject\App.config" Vital="yes" DiskId="1" />  

for modify the App.config have to use util:XmlFile tag. in here my app.config file is something like this.

 <?xml version="1.0" encoding="utf-8" ?>  
 <configuration>  
  <appSettings>  
   <add key="TextLocation" value="d:\ScheduledServiceNew.txt"/>  
  </appSettings>  
 </configuration>  
I want to update it into something like this:

 <?xml version="1.0" encoding="utf-8"?>  
 <configuration>  
  <appSettings>  
   <add key="TextLocation" value="C:\Program Files (x86)\Installer\TestService\ScheduledServiceNew.txt"/>  
  </appSettings>  
 </configuration>  

for that i have to use  util:XmlFile tag as follows:
 <util:XmlFile Id="ModifyServiceLocation" ElementPath="//configuration/appSettings/add[\[]@key='TextLocation'[\]]/@value" File="[TestService]\TestInstallerProject.exe.config" Action="setValue" Value="[TestService]ScheduledServiceNew.txt" PreserveModifiedDate="yes" SelectionLanguage="XPath" Sequence="1" />  

once you compile the project you can create installer using wix for windows service