2

I'm facing a weird problem and I can't find a working solution. Thanks in advance for any help.

I developed a Windows Service in C# that use Newtonsoft.JSON library to parse the result from a bunch of WebAPI. The service is deployed with MSI and everything was working perfectly.

The first version of the service was using Newtonsoft.JSON 6.0.8, but recently I moved to version 11.0.2.

I packed a new MSI (with correct version/Product Id/Upgrade Id to ensure upgrading) and I'm trying to deploy the new version through a small install application that is performing the following:

  1. Stop the service (if installed and running) with a System.ServiceProcess.ServiceController
  2. Uninstall the service with a System.ServiceProcess.ServiceController
  3. Run a System.Process that invokes msiexec on the new msi file

The result: the service is not working, and from the log, I can see that the application is still looking for the 6.0.8 version of Newtonsoft.JSON.

If I try the following:

  1. Right-click on msi -> Uninstall
  2. Right-click on msi -> Install

Everything is working fine and the service uses the version 11.0.2 of the library...

I'm going crazy about this... How can I fix/clean the upgrade process?

Thanks in advance

Andrea
  • 11,801
  • 17
  • 65
  • 72
Defkon1
  • 492
  • 4
  • 15

1 Answers1

1

Summary: Modify and extend your MSI to handle all service related tasks: service installation, service deletion, service control. Use the built-in MSI constructs to do so.


Service Installation: Not sure I followed the whole problem scenario description, but the deal is that you should let the MSI itself control the service installation and service control during the installation and upgrade process. Services are installed and controlled in MSI files via the ServiceInstall and ServiceControl tables - that map directly to WiX XML Elements as illustrated here:

<Component>
   <File Source="$(var.SourceDir)\WindowsService.exe" />
   <ServiceInstall Name="MyService" ErrorControl="normal" Start="auto" Type="ownProcess" />
   <ServiceControl Id="MyService" Name="MyService" Start="install" Stop="both" Remove="uninstall" Wait="yes" />
</Component>

Let me link to a similar sample on github (by Rainer Stropek) in case the above is not clear. It is more complete and elaborate.


Links: This answer discusses the same issue: Service Installation & Control. Using custom actions or custom executables to deal with service control and installation or uninstallation is considered a deployment anti-pattern - it is neither necessary, desirable nor reliable. MSI is full-featured and reliable once used right (unless you have a very special and unusual service that needs custom actions "for some reason". Correction, new rectification follows: "for some very good reason").

Stein Åsmul
  • 39,960
  • 25
  • 91
  • 164
  • actually I'm using VS to generate MSI (and honestly it always worked)... do you think I have to move to Wix? – Defkon1 Aug 30 '18 at 12:48
  • 1
    VS Installer Projects are rather legendary for quirks and limitations - and some of them are quite severe. These projects tend to work for a while until new and unavoidable requirements come along that are suddenly almost impossible to deal with. Moving to WiX might be a safer bet - it all depends on your circumstances - particularly your own control of the application - or lack thereof. If you have some WiX knowledge already, you can always decompile any MSI to WiX markup using the **`dark.exe`** MSI decompiler. The decompile will always require substantial massage to work as WiX source. – Stein Åsmul Aug 30 '18 at 12:52
  • I've used Wix for a couple of project in the past... I will to try to convert VS project to Wix in this context, too... – Defkon1 Aug 30 '18 at 13:13
  • After decompile, please remember to make sure to take out service custom actions and replace with the above ServiceInstall and ServiceControl constructs. Good luck. It can get fiddly, but with WiX you have fine grained control for the future. – Stein Åsmul Aug 30 '18 at 13:15
  • I wrote the Wix project from scratch (just to be sure to have a clean result) but actually I have an error about permissions when the service is started... – Defkon1 Aug 31 '18 at 08:38
  • My fragment: – Defkon1 Aug 31 '18 at 08:40
  • The ComponentGroupRef=Items is harvested when building from the MyPoller service solution bin folder – Defkon1 Aug 31 '18 at 08:41
  • What is the error message about permissions? The event log on the system should have some clues? Does the service run as LocalSystem? – Stein Åsmul Aug 31 '18 at 08:44
  • Solved. The ServiceInstall didn't found the .exe from the harvested fragment, so I have added a transforms to purge the exe file from the harvest, manually added to my wxs (with KeyPath=yes) and rebuilt. – Defkon1 Aug 31 '18 at 09:01
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/179167/discussion-between-stein-asmul-and-defkon1). – Stein Åsmul Aug 31 '18 at 09:16