4

I have written a bare-bones .NET 4.7.2 C# Windows Service using TopShelf and Quartz. The service works when I debug it using Visual Studio 2019 on my Windows 10 laptop. I then created a Wix 3.11.2 based setup to install and start this service. Now, I am trying to install the service on my laptop using this installer. The installer is able to copy the files but fails to start the service. Here is the code:

Product.wxs

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Product Id="*" Name="Broker Test" Language="1033" Version="1.0.0.0"
       Manufacturer="Test"
       UpgradeCode="{68813F65-1022-4E32-AC50-CD16B5927DAD}">
<Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />

<Media Id="1" Cabinet="BrokerTest.cab" EmbedCab="yes" />

<Directory Id="TARGETDIR" Name="SourceDir">
  <Directory Id="INSTALLDIR" Name="Application">
    <Directory Id="WINDIR" Name="Service"/>
  </Directory>
</Directory>

<Feature Id="ProductFeature" Title="BrokerTest_MSI" Level="1">
  <ComponentGroupRef Id="BrokerWindowsService" />
</Feature>

<Property Id="WIXUI_INSTALLDIR" Value="INSTALLDIR" />
<Property Id="INSTALLTYPE" Value="1"/>
</Product>
</Wix>

WinService.wxs

<?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>
<ComponentGroup Id="BrokerWindowsService">

  <ComponentRef Id="Tb.WinService.Test" />
  <ComponentRef Id="TopShelf"/>
  <ComponentRef Id="Tb.ServiceConfig" />
  <ComponentRef Id="Microsoft.Extensions.DependencyInjection" />
  <ComponentRef Id="Microsoft.Extensions.DependencyInjection.Abstractions" />      
  <ComponentRef Id="Quartz" />      

</ComponentGroup>

<DirectoryRef Id="WINDIR">
  <Directory Id="DataDir" Name="Data"/>

  <Component Id="Tb.WinService.Test" Guid="{F1DF09D9-98D8-4D63-9BB9-7581D56E1685}">

    <CreateFolder Directory="DataDir">
      <util:PermissionEx User="NT Authority\SYSTEM" GenericAll="yes"/>
    </CreateFolder>

    <File Id="Tb.WinService.Test.dll" Name="$(var.Tb.WinService.Test.TargetFileName)" Source="$(var.Tb.WinService.Test.TargetPath)" />
    <File Id="Tb.WinService.Test.pdb" Name="$(var.Tb.WinService.Test.TargetName).pdb" Source="$(var.Tb.WinService.Test.TargetDir)$(var.Tb.WinService.Test.TargetName).pdb" />

    <ServiceInstall Id="ServiceInstaller" Type="ownProcess" Vital="yes"
                            Name="BrokerWindowsServiceTest"
                            DisplayName="Broker Windows Service Test"
                            Description="Runs scheduled tasks"
                            Start="auto" Account="NT Authority\SYSTEM"
                            ErrorControl="ignore"
                            Interactive="no" />

    <ServiceControl Id="StartService"
                            Start="install" Stop="both" Remove="uninstall"
                            Name="BrokerWindowsServiceTest" Wait="yes" />

  </Component>

  <Component Id="TopShelf" Guid="{CEC3596B-4D38-4641-81EF-CBC09C4FE67E}">
    <File Id="TopShelf.dll" Name="TopShelf.dll" Source="$(var.SolutionDir)packages\Topshelf.4.2.1\lib\net452\Topshelf.dll" Vital="yes" />
    <File Id="TopShelf.xml" Name="TopShelf.xml" Source="$(var.SolutionDir)packages\Topshelf.4.2.1\lib\net452\Topshelf.xml" Vital="yes" />
  </Component>

  <Component Id="Tb.ServiceConfig" Guid="{A08D1440-4B2A-4DFB-9F55-27E81DC4B106}">
    <File Id="Tb.Service.App.Config" Name="$(var.Tb.WinService.Test.TargetName).exe.config" 
          Vital="yes" KeyPath="yes" Source="$(var.Tb.WinService.Test.TargetDir)App.config" />
  </Component>

  <Component Id="Microsoft.Extensions.DependencyInjection" Guid="{8F69CFC4-02F9-479C-9C57-E2F88180E542}">
    <File Id="Microsoft.Extensions.DependencyInjection.dll" Name="Microsoft.Extensions.DependencyInjection.dll" 
          Source="$(var.SolutionDir)packages\Microsoft.Extensions.DependencyInjection.3.0.1\lib\netstandard2.0\Microsoft.Extensions.DependencyInjection.dll" Vital="yes" />
  </Component>

  <Component Id="Microsoft.Extensions.DependencyInjection.Abstractions" Guid="{8EB47289-9527-4CE4-9991-EBF8997368DC}">
    <File Id="Microsoft.Extensions.DependencyInjection.Abstractions.dll" Name="Microsoft.Extensions.DependencyInjection.Abstractions.dll" 
          Source="$(var.SolutionDir)packages\Microsoft.Extensions.DependencyInjection.Abstractions.3.0.1\lib\netstandard2.0\Microsoft.Extensions.DependencyInjection.Abstractions.dll" Vital="yes" />
  </Component>

  <Component Id="Quartz" Guid="{345DA6DA-F385-47A6-844C-3171ADE42E83}">
    <File Id="Quartz.dll" Name="Quartz.dll" Source="$(var.SolutionDir)packages\Quartz.3.0.7\lib\net452\Quartz.dll" Vital="yes" />
    <File Id="Quartz.xml" Name="Quartz.xml" Source="$(var.SolutionDir)packages\Quartz.3.0.7\lib\net452\Quartz.xml" Vital="yes" />
  </Component>      

</DirectoryRef>
</Fragment>
</Wix>

I get the following error when I run the Wix Installer:

Privileges Error

If I try to start the service manually, I get the following error:

Error Message

I have already tried the following:

  1. I am using the NT Authority\System account which should have the proper access.
  2. Currently my windows service isn't really doing anything. It's returing Task.CompletedTask. So, there shouldn't be a timeout scenario.
  3. I have tried with both debug and release builds.
  4. I am installing on the same machine I am developing on. So, .NET framework version shouldn't be an issue.
  5. I tried launching the debugger but it was unable to attach. So I didn't get any more details.
  6. I caught exceptions and wrote to the Event Log but that too doesn't have any more details.
Yasir
  • 1,595
  • 5
  • 23
  • 42
  • Have you tried to look at the MSI log? Did you see any errors in file? – Pavel Anikhouski Nov 27 '19 at 18:48
  • Or check the event log while restarting the windows service. – Eldar Nov 27 '19 at 18:48
  • @PavelAnikhouski The log shows the following error "Error 1923. Service 'Broker Windows Service Test' (BrokerWindowsServiceTest) could not be installed. Verify that you have sufficient privileges to install system services." – Yasir Nov 27 '19 at 18:57
  • @Eldar I checked the Event log. It was the same error that I am seeing elsewhere: "Error 1923. Service 'Broker Windows Service Test' (BrokerWindowsServiceTest) could not be installed. Verify that you have sufficient privileges to install system services." – Yasir Nov 27 '19 at 18:59
  • Possibly it is not Wix installer issue. I wold try to install the service using InstallUtil command. (run as admin) and check the log if you get errors. – Michael Daniloff Nov 27 '19 at 19:25
  • @MichaelDaniloff I directly navigated to the exe location in the bin/release folder on my machine and installed and started the service using the command prompt and it worked without issues. But it doesn't start when I install it through Wix. – Yasir Nov 27 '19 at 19:48
  • @Yasir but can you do the same for the location where Wix put it? – Michael Daniloff Nov 27 '19 at 20:05
  • @MichaelDaniloff If I navigate to the location where Wix has dumped the files and then install the service using the command prompt, it works. I can then go and manually start the service as well. – Yasir Nov 27 '19 at 20:48
  • @MichaelDaniloff Another interesting thing if I install the service from the Wix location manually, and then click retry in the Wix installer, I am able to start the service. So, the answer must be related to user access in Wix vs manual install. – Yasir Nov 27 '19 at 20:57
  • Does it work if you use "NT AUTHORITY" instead of "NT Authority". I have some vague memory of that making a difference – Brian Sutherland Nov 27 '19 at 22:30

2 Answers2

2

Make sure the ServiceInstall Name exactly matches the name of your topshelf service

0

Installer Methods: I would inspect your service installer code inside the binary. There must be something that you are doing in the installation methods for the assembly / binary that is not replicated by MSI at installation time. What are you doing in these installer methods? Samples: 1 and 2.

WiX Service Installation: I would take out all attributes that are not needed in the WiX element. See this sample: Service Installation (Stropek). Particularly the Account attribute, just leave it out. Also take out that permissioning element for SYSTEM. Default permissions are better - they are already there.

Procmon.exe: You can inspect the system for what happens when you run InstallUtil.exe if you run Procmon.exe and inspect the information overload. Are you familiar with this tool? I would prefer not to go into it (again - rudimentary example), and I think it should be enough to study you installation method code instead.

Capture: It is also possible to use a setup-capture tool to scan the before and after state of the system when you run InstallUtil.exe. That requires a hefty tool (AdminStudio, Advanced Installer) and is rarely available for developers. Just for the record.


Links:

Additional:

Stein Åsmul
  • 39,960
  • 25
  • 91
  • 164