The .NET Service
I have been scratching my head for quite some time to try and understand as to why exactly my installation fails with this error.
I created a basic .NET 4.8 windows service that have real basic functionality
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Test
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
#if DEBUG
Service1 test = new Service1();
test.OnDebug();
Thread.Sleep(Timeout.Infinite);
#else
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
#endif
}
}
}
using Serilog;
using System;
using System.ServiceProcess;
namespace Test
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
Log.Logger = new LoggerConfiguration()
.WriteTo.File("<Log path>", rollingInterval: RollingInterval.Day)
.MinimumLevel.Information()
.CreateLogger();
}
public void OnDebug()
{
try
{
OnStart(null);
}
catch (Exception ex)
{
Log.Error(ex, ex.Message);
}
}
protected override void OnStart(string[] args)
{
try
{
Log.Information("Service Started");
}
catch (Exception ex)
{
Log.Error(ex, ex.Message);
}
}
protected override void OnStop()
{
try
{
Log.Information("Service Stopped");
}
catch (Exception ex)
{
Log.Error(ex, ex.Message);
}
}
}
}
The WIX Project
I created my basic Wix project and added the service as a reference like usual.
Below is the Package.wxs file
<?xml version="1.0" encoding="UTF-8"?>
<?define Name = "Test" ?>
<?define Manufacturer = "Test" ?>
<?define Version = "1.0.0.0" ?>
<?define UpgradeCode = "1D38B1B5-0BD5-419B-9BAC-9F571D2F8048" ?>
<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs">
<Package
Name="$(Name)"
Manufacturer="$(Manufacturer)"
Version="$(Version)"
UpgradeCode="$(var.UpgradeCode)"
Compressed="yes">
<MajorUpgrade DowngradeErrorMessage="A later version is already installed." />
<Media Cabinet="cab1.cab" Id="1" EmbedCab="yes"></Media>
<Feature Id="Service" Title="Test Service" Level="1">
<ComponentRef Id="ServiceExecutable" />
</Feature>
</Package>
</Wix>
My Components.wxs file:
<?xml version="1.0" encoding="UTF-8"?>
<?define Name = "Test Service" ?>
<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs">
<Fragment>
<ComponentGroup Id="Components" Directory="INSTALLFOLDER">
<Component Id="ServiceExecutable" Bitness="always64">
<File Id="Test.exe"
Source="$(var.Test.TargetDir)Test.exe"
KeyPath="true" />
<RemoveFile Id="ALLFILES" Name="*.*" On="both" />
<ServiceInstall Id="ServiceInstaller"
Name="Service1"
Type="ownProcess"
DisplayName="$(Name)"
Description="This is a test service"
Start="disabled"
ErrorControl="normal" />
<ServiceControl Id="StartService"
Name="Service1"
Start="install"
Stop="both"
Remove="uninstall"
Wait="true" />
</Component>
</ComponentGroup>
</Fragment>
</Wix>
And my Folders.wxs file
<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs">
<Fragment>
<StandardDirectory Id="ProgramFiles6432Folder">
<Directory Id="INSTALLFOLDER" Name="!(bind.Property.Manufacturer) !(bind.Property.ProductName)" />
</StandardDirectory>
</Fragment>
</Wix>
When installing my service without the Service Control the service gets installed correctly and runs fine, however this makes the installer pretty useless since I don’t have the functionality of the installer.
The error only appears to be happening when I run it with the Service Control added.
I troubleshooted a bit with the log file and couldn’t see anything out of the ordinary.
I ran the .msi file with PowerShell as admin and still came across this issue
I appended the Account attribute to the Service Install, that also didn’t seem to make a difference.
I followed steps to add the account to the service group policies, but with no luck, still the same error. (See link below for steps I took)
https://www.blackfog.com/knowledge-base/privileges-to-install-system-services/
At this stage I am ready to give up.
Any help would be greatly appreciated