title: WiX MajorUpgrade of Windows Service, preserving .config, and avoiding a reboot
body: <p>I am struggling to get MajorUpgrade, ServiceControl, .config files to work nicely together.  <a href="https://stackoverflow.com/questions/22770414/wix-majorupgrade-schedule-afterinstallexecute-wont-overwrite-even-non-keypath">After my other question</a>, I'm now kinda having the opposite problem again.  </p>
  
  <p>Before, the files weren't being overwritten because the AssemblyFileVersions were static so I fixed that.  <strong>1)</strong> Now, even with <code>Schedule="afterInstallExecute"</code> my <code>KeyPath='yes'</code>  <em>.config</em> file is <strong>still being overwritten</strong> even though the existing file modified date is different than the file creation date and it is set as a KeyPath.  I'm currently having to overwrite the <em>.config</em> file and restart the service after the install.</p>
  
  <p><strong>2)</strong> And even if I fix that, I still have a problem of <strong>avoiding a reboot</strong>.  If I say <code>Schedule="afterInstallInitialize"</code> then I believe the .config file will certainly be removed along with the service too early.  If I say <code>Schedule="afterInstallExecute"</code> then the service isn't stopped and after the install a reboot is necessary.  (That's right, right?)  Stopping the service manually prior to the install let's me avoid the reboot.  Adding a <code>net stop</code> custom action could work to replace the <code>ServiceControl</code> I guess, but getting all the conditions right seems complex.</p>
  
  <p><strong>3)</strong> As a bonus, I'd like to <strong>NOT remove the service at all during an upgrade</strong>.  Can I just stop the service, replace the binary, and start the service again?  That will <strong>avoid re-entering the service account credentials for an upgrade</strong>.  But of course it still needs to install on a first install and uninstall on a feature removal.</p>
  
  <p>Here's the meat of it (which is also bundled later, in case that somehow matters):</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;MajorUpgrade DowngradeErrorMessage="A newer version is already installed." 
                Schedule="afterInstallExecute" /&gt;
  
  &lt;ComponentGroup Id="ServiceCG"&gt;
      &lt;Component Id="Service" Guid='*' Win64='yes' Directory='INSTALLDIR'&gt;
          &lt;File Id='ServiceEXE' Source='$(var.root)Service.exe' /&gt;
          &lt;ServiceInstall Id="ServiceInstall"
                            Name="MyService"
                            DisplayName="My Server"
                            Type="ownProcess"
                            Start="auto"
                            ErrorControl="normal"
                            Description="My Server Service"
                            Interactive="no"
                            Account="[...]"
                            Password="[...]" /&gt;
          &lt;ServiceControl Id="StopService" Name="MyService" Start="install" 
                          Stop="uninstall" Wait="yes" Remove="both" /&gt;
          &lt;util:User Id="UpdateServiceAccountLogonAsService" UpdateIfExists="yes"
                     CreateUser="no" Name="[SERVICEACCOUNTFULL]" 
                     LogonAsService="yes"/&gt;
      &lt;/Component&gt;
      &lt;Component Id="ServiceConfig" Guid='*' Win64='yes' Directory='INSTALLDIR'&gt;
          &lt;File Id='FileServiceConfig' KeyPath='yes' 
                Source='$(var.root)Service.exe.config' /&gt;
      &lt;/Component&gt;
  &lt;/ComponentGroup&gt;
  </code></pre>
  
  <p>Related but unanswered: </p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/22117138/prevent-service-removal-install-during-wix-major-upgrade-service-not-stopping">Prevent service removal/install during WiX major upgrade - service not stopping</a></li>
  </ul>
  
  <p>WiX version 3.8.1128.0</p>