void StartService(HostControl hostControl, ServiceControl service) { if (hostControl == null) throw new ArgumentNullException("hostControl"); if (service == null) return; _log.InfoFormat("Starting Service {0}", service); if (!service.Start(hostControl)) throw new TopshelfException(string.Format((string)"Failed to start service: {0}", (object)service)); }
public static extern bool ControlService(SafeServiceHandle hService, ServiceControl dwControl, ref SERVICE_STATUS lpServiceStatus);
private static int ServiceHandler(ServiceControl control, int eventType, IntPtr eventData, IntPtr context) { Logger.Log(LogLevel.Debug, "Received control {0} eventType {1} eventData {2} context {3}", control.ToString(), eventType, eventData.ToString(), context.ToString()); OldNativeServiceDispatcher service = (OldNativeServiceDispatcher)GCHandle.FromIntPtr(context).Target; try { switch (control) { case ServiceControl.Interrogate: return 0; case ServiceControl.Stop: return service.OnControl(() => service.OnStop(), ServiceState.StopPending, ServiceState.Stopped); case ServiceControl.Pause: return service.OnControl(() => service.OnPause(), ServiceState.PausePending, ServiceState.Paused); case ServiceControl.Continue: return service.OnControl(() => service.OnContinue(), ServiceState.ContinuePending, ServiceState.Running); case ServiceControl.PowerEvent: return service.OnControl(() => service.OnPowerEvent(eventType, eventData)); case ServiceControl.Shutdown: return service.OnControl(() => service.OnShutdown()); default: return service.OnControl(() => service.OnCustomEvent(control, eventType, eventData)); } } catch (Win32Exception ex) { Logger.Log(LogLevel.Warning, "Win32 error {0} handling control {1}", ex.NativeErrorCode, control.ToString()); return ex.NativeErrorCode; } catch (Exception ex) { Logger.Log(LogLevel.Error, "Uncaught exception {0}\n{1}", ex.Message, ex.ToString()); return 31; } }
protected virtual void OnCustomEvent(ServiceControl control, int eventType, IntPtr eventData) { throw new NotSupportedException(); }
protected static extern bool ControlService( NativeService hService, ServiceControl dwControl, out ServiceStatus lpServiceStatus );
private static extern int ControlService(IntPtr hService, ServiceControl dwControl, SERVICE_STATUS lpServiceStatus);
public LifetimeScopeServiceControl(ILifetimeScope lifetimeScope, ServiceControl service, string serviceName) { _lifetimeScope = lifetimeScope; _service = service; _serviceName = serviceName; }
void StopService(HostControl hostControl, ServiceControl service) { if (hostControl == null) throw new ArgumentNullException("hostControl"); if (service == null) return; try { _log.InfoFormat("Stopping Service {0}", service); if (!service.Stop(hostControl)) throw new TopshelfException(string.Format((string)"Failed to stop service: {0}", (object)service)); } catch (Exception ex) { _log.Error("Stop Service Failed", ex); } }
void StartService(HostControl hostControl, ServiceControl service) { if (hostControl == null) throw new ArgumentNullException(nameof(hostControl)); if (service == null) return; _log.InfoFormat("Starting Service {0}", service); if (!service.Start(hostControl)) throw new TopshelfException($"Failed to start service: {service}"); }
public SelectService(ServiceControl actualService) { _actualService = actualService; }
private static extern int ControlService(IntPtr hService, ServiceControl dwControl, ServiceStatus lpServiceStatus);
public static extern bool ControlService(IntPtr hService, ServiceControl dwControl, ref ServiceStatus lpServiceStatus);