public KinesisTapService() { this.serviceManager = new KinesisTapServiceManager(); // Try to enable pre-shutdown notifications to give KT an early start on shutdown. // By enabling this, as soon as a shutdown is triggered in the OS, KT will be notified. // https://docs.microsoft.com/en-us/windows/win32/services/service-control-handler-function try { // Unfortunately there's no convenience property for enabling pre-shutdown notifications, so we have to do it via reflection. // http://www.sivachandran.in/2012/03/handling-pre-shutdown-notification-in-c.html var acceptedCommandsFieldInfo = typeof(ServiceBase).GetField("acceptedCommands", BindingFlags.Instance | BindingFlags.NonPublic); if (acceptedCommandsFieldInfo != null) { int value = (int)acceptedCommandsFieldInfo.GetValue(this); acceptedCommandsFieldInfo.SetValue(this, value | SERVICE_ACCEPT_PRESHUTDOWN); } } catch (Exception ex) { // If this fails, we'll just log an error and ignore. // Since we're setting internal field properties, Windows may change this at any time. this.EventLog.WriteEntry($"An error occurred trying to configure the Service to accept pre-shutdown notifications: {ex}", EventLogEntryType.Information); } // Enable the ability to subscribe to shutdown events. // This instructs the Service Control Manager to call the "OnShutdown" method when the OS is shutting down. // When this is called, it only has 5 seconds to stop. // This is enabled as a backup in case the pre-shutdown notification is missed. this.CanShutdown = true; }
public KinesisTapService() { this.parameterStore.StoreConventionalValues(); // configure logging var nlogConfigPath = parameterStore.GetParameter(HostingUtility.NLogConfigPathKey); NLog.LogManager.LoadConfiguration(nlogConfigPath); this.serviceLoggerFactory = new LoggerFactory() .AddEventLog(new EventLogSettings { SourceName = KinesisTapServiceManager.ServiceName, LogName = "Application", Filter = (msg, level) => level >= LogLevel.Information }) .AddNLog(); logger = this.serviceLoggerFactory.CreateLogger <KinesisTapService>(); this.serviceManager = new KinesisTapServiceManager(this.typeLoader, this.parameterStore, this.serviceLoggerFactory.CreateLogger <KinesisTapServiceManager>(), new DefaultNetworkStatusProvider()); // Try to enable pre-shutdown notifications to give KT an early start on shutdown. // By enabling this, as soon as a shutdown is triggered in the OS, KT will be notified. // https://docs.microsoft.com/en-us/windows/win32/services/service-control-handler-function try { // Unfortunately there's no convenience property for enabling pre-shutdown notifications, so we have to do it via reflection. // http://www.sivachandran.in/2012/03/handling-pre-shutdown-notification-in-c.html var acceptedCommandsFieldInfo = typeof(ServiceBase).GetField("acceptedCommands", BindingFlags.Instance | BindingFlags.NonPublic); if (acceptedCommandsFieldInfo != null) { int value = (int)acceptedCommandsFieldInfo.GetValue(this); acceptedCommandsFieldInfo.SetValue(this, value | SERVICE_ACCEPT_PRESHUTDOWN); } // when 'preshutdown' is enabled, we need to disable 'shutdown' command so that shutdown event is not handled twice this.CanShutdown = false; } catch (Exception ex) { // If this fails, we'll just log an error and ignore. // Since we're setting internal field properties, Windows may change this at any time. this.EventLog.WriteEntry($"An error occurred trying to configure the Service to accept pre-shutdown notifications: {ex}", EventLogEntryType.Information); // Set this to 'true' so we can handle OS's SHUTDOWN command this.CanShutdown = true; } }
private void btnStart_Click(object sender, EventArgs e) { btnStart.Enabled = false; _parameterStore.StoreConventionalValues(); var nlogConfigPath = _parameterStore.GetParameter(HostingUtility.NLogConfigPathKey); NLog.LogManager.LoadConfiguration(nlogConfigPath); // configure logging _serviceManager = new KinesisTapServiceManager(new PluginLoader(), _parameterStore, _serviceLoggerFactory.CreateLogger <KinesisTapServiceManager>(), new DefaultNetworkStatusProvider()); _serviceManager.Start(); btnStop.Enabled = true; }