Controlling Application Lifetime In Topshelf
January 20, 2010
One of Topshelf’s best features is the ability to easily host one or more services running side by side. In this post we will create a Topshelf service which will shut down the application. Lets take a look at how it is done. Coordinating the Service Let’s create a fake Health Monitoring service which will, sometime in the future, tell Topshelf to shut the application down. The Topshelf hook for controlling the state of all the services it is hosting is a type called IServiceCoordinator. Here is the code for our health monitor. public class HealthMonitoringService : IHealthMonitoringService { private readonly IServiceCoordinator _serviceCoordinator; private readonly ILogger _logger; private Timer _timer; private int _healthCheckCount = 0; public HealthMonitoringService(IServiceCoordinator serviceCoordinator, ILogger logger) { _serviceCoordinator = serviceCoordinator; _logger = logger; } public void Start() { _logger.LogWarn("The Health Monitor is starting up");…