I don't use Topshelf as a service host, so I don't use topshelf.host.exe.
here is how I use it: HostFactory.Run(
c =>
{
c.Service<IMyService>(
s =>
{
s.ConstructUsing(() => kernel.Get<IMyService>());
s.WhenStarted(service => service.Start());
s.WhenStopped(service => service.Stop());
});
c.RunAsLocalSystem();
c.SetDescription(WindowsServiceDescription);
c.SetDisplayName(WindowsServiceName);
c.SetServiceName(WindowsServiceName);
c.StartAutomatically();
});
[RunInstaller(true)]
public class WindowsServiceInstaller : Installer
{
const string AssemblyIdentifier = "MyServiceAssembly";
const string InstallUtilAssemblyParameter = "assemblypath";
const string ArgumentInstall = "install";
const string ArgumentUninstall = "uninstall";
public override void Install(IDictionary stateSaver)
{
var topshelfAssembly = Context.Parameters[InstallUtilAssemblyParameter];
stateSaver.Add(AssemblyIdentifier, topshelfAssembly);
RunHidden(topshelfAssembly, ArgumentInstall);
base.Install(stateSaver);
}
public override void Uninstall(IDictionary savedState)
{
var topshelfAssembly = savedState[AssemblyIdentifier].ToString();
RunHidden(topshelfAssembly, ArgumentUninstall);
base.Uninstall(savedState);
}
static void RunHidden(string primaryOutputAssembly, string arguments)
{
var startInfo = new ProcessStartInfo(primaryOutputAssembly, arguments)
{
WindowStyle = ProcessWindowStyle.Hidden,
};
using (var process = Process.Start(startInfo))
{
process.WaitForExit();