Skip to content

Instantly share code, notes, and snippets.

@PaulNichols
Created August 21, 2017 23:54
Show Gist options
  • Save PaulNichols/a64da996fd09d4a6dec3fb6c7582bc6a to your computer and use it in GitHub Desktop.
Save PaulNichols/a64da996fd09d4a6dec3fb6c7582bc6a to your computer and use it in GitHub Desktop.
Topshelf Windows Service configuration
using Topshelf;
internal static class ConfigureService
{
internal static void Configure(<ServiceType> serviceName)
{
HostFactory.Run(configure =>
{
configure.UseSerilog();
configure.Service<ServiceType>(x =>
{
x.ConstructUsing(name => serviceName);
x.WhenStarted(tc => tc.Start());
x.WhenStopped(tc => tc.Stop());
});
configure.RunAsLocalSystem();
configure.SetServiceName("Site Management Service");
configure.SetDisplayName("Site Management Service");
configure.SetDescription("Site Management Service by Mexia");
configure.EnableServiceRecovery(r =>
{
r.RestartService(1);
r.OnCrashOnly();
});
configure.EnableServiceRecovery(r =>
{
r.OnCrashOnly();
r.RestartService(1); //first
r.RestartService(1); //second
r.RestartService(1); //subsequents
r.SetResetPeriod(0);
});
string siteId = "";
configure.AddCommandLineDefinition("SiteId", s => siteId = s);
configure.BeforeInstall(() =>
{
});
configure.AfterInstall(() =>
{
});
configure.StartAutomatically();
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment