Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Misiu/70b6015ac11641923dbe5b1a197a18e8 to your computer and use it in GitHub Desktop.
Save Misiu/70b6015ac11641923dbe5b1a197a18e8 to your computer and use it in GitHub Desktop.
Topshelf custom environment builder
class FooEnvironmentBuilder : EnvironmentBuilder
{
readonly HostConfigurator _configurator;
WindowsHostEnvironment _windowHostEnvironment;
public FooEnvironmentBuilder(HostConfigurator configurator)
{
_configurator = configurator;
_windowHostEnvironment = new WindowsHostEnvironment(_configurator);
}
public HostEnvironment Build()
{
return new FooHostEnvironment(_windowHostEnvironment, _configurator);
}
}
public class FooHostEnvironment : HostEnvironment
{
readonly WindowsHostEnvironment _windowsHostEnvironment;
readonly HostConfigurator _configurator;
public FooHostEnvironment(WindowsHostEnvironment windowsHostEnvironment, HostConfigurator configurator)
{
_windowsHostEnvironment = windowsHostEnvironment;
_configurator = configurator;
}
public bool IsServiceInstalled(string serviceName)
{
return _windowsHostEnvironment.IsServiceInstalled(serviceName);
}
public bool IsServiceStopped(string serviceName)
{
return _windowsHostEnvironment.IsServiceStopped(serviceName);
}
public void StartService(string serviceName, TimeSpan startTimeOut)
{
_windowsHostEnvironment.StartService(serviceName, startTimeOut);
}
public void StopService(string serviceName, TimeSpan stopTimeOut)
{
_windowsHostEnvironment.StopService(serviceName, stopTimeOut);
}
public void InstallService(InstallHostSettings settings, Action<InstallHostSettings> beforeInstall, Action afterInstall, Action beforeRollback,
Action afterRollback)
{
_windowsHostEnvironment.InstallService(settings, beforeInstall, afterInstall, beforeRollback, afterRollback);
}
public void UninstallService(HostSettings settings, Action beforeUninstall, Action afterUninstall)
{
_windowsHostEnvironment.UninstallService(settings, beforeUninstall, afterUninstall);
}
public bool RunAsAdministrator()
{
return _windowsHostEnvironment.RunAsAdministrator();
}
public Host CreateServiceHost(HostSettings settings, ServiceHandle serviceHandle)
{
return new FooServiceHost(this, settings, serviceHandle, _configurator);
}
public void SendServiceCommand(string serviceName, int command)
{
_windowsHostEnvironment.SendServiceCommand(serviceName, command);
}
public string CommandLine => _windowsHostEnvironment.CommandLine;
public bool IsAdministrator => _windowsHostEnvironment.IsAdministrator;
public bool IsRunningAsAService => _windowsHostEnvironment.IsRunningAsAService;
}
class Program
{
static int Main()
{
return (int)HostFactory.Run(c =>
{
c.UseEnvironmentBuilder(FooEnvironmentBuilder);
c.Service<Program>(s =>
{
s.ConstructUsing(name => new Program());
s.WhenStarted((p, hostControl) =>
{
// ...
});
s.WhenStopped((p, hostControl) =>
{
// ...
});
});
c.RunAsNetworkService();
c.SetServiceName("foo");
c.SetDisplayName("Foo Service");
});
}
static EnvironmentBuilder FooEnvironmentBuilder(HostConfigurator configurator)
{
return new FooEnvironmentBuilder(configurator);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment