/// <summary> /// Set configuration data /// </summary> /// <param name="serviceImplementation">The service with configuration settings.</param> private void ConfigureServiceFromAttributes(IWindowsService serviceImplementation) { var attribute = serviceImplementation.GetType().GetAttribute <WindowsServiceAttribute>(); if (attribute != null) { // wire up the event log source, if provided if (!string.IsNullOrWhiteSpace(attribute.EventLogSource)) { // assign to the base service's EventLog property for auto-log events. EventLog.Source = attribute.EventLogSource; } CanStop = attribute.CanStop; CanPauseAndContinue = attribute.CanPauseAndContinue; CanShutdown = attribute.CanShutdown; // we don't handle: laptop power change event CanHandlePowerEvent = false; // we don't handle: Term Services session event CanHandleSessionChangeEvent = false; // always auto-event-log AutoLog = true; } else { throw new InvalidOperationException( string.Format("IWindowsService implementer {0} must have a WindowsServiceAttribute.", serviceImplementation.GetType().FullName)); } }
private void ConfigureServiceFromAttributes(IWindowsService ServiceImplementation) { var attribute = ServiceImplementation.GetType().GetAttribute<ServiceAttribute>(); if (attribute != null) { // wire up the event log source, if provided if (!string.IsNullOrWhiteSpace(attribute.EventLogSource)) { // assign to the base service's EventLog property for auto-log events. EventLog.Source = attribute.EventLogSource; } CanStop = attribute.CanStop; CanPauseAndContinue = attribute.CanPauseAndContinue; CanShutdown = attribute.CanShutdown; // we don't handle: laptop power change event CanHandlePowerEvent = false; // we don't handle: Term Services session event CanHandleSessionChangeEvent = false; // always auto-event-log AutoLog = true; } else { throw new InvalidOperationException( string.Format("IWindowsService implementer {0} must have a WindowsServiceAttribute.", ServiceImplementation.GetType().FullName)); } }
// Run a service from the console given a service implementation internal static void Run(string[] args, IWindowsService service) { string serviceName = service.GetType().Name; bool isRunning = true; // simulate starting the windows service // bypass the system event call via ServiceBase and go straight to our handler if (service.OnStart(args, new EventLog(true))) { // let it run as long as Q is not pressed while (isRunning) { WriteToConsole(ConsoleColor.Yellow, "\nEnter either [P]ause, [R]esume, [Q]uit (stop), [S]hutdown: "); isRunning = HandleConsoleInput(service, WaitForKey(false)); } if (shutdown) { service.OnShutdown(); } else { service.OnStop(); } } WaitForKey(true); }
public static void Run(string[] args, IWindowsService service) { string serviceName = service.GetType().Name; bool isRunning = true; service.OnStart(args); while (isRunning){ WriteToConsole(ConsoleColor.Yellow, "Enter either [Q]uit, [P]ause, [R]esume : "); isRunning = HandleConsoleInput(service, Console.ReadLine()); } // stop service.OnStop(); }
private void ConfigureServiceFromAttributes(IWindowsService serviceImplementation) { var attribute = serviceImplementation.GetType().GetAttribute <WindowsServiceConfigurationAttribute>(); if (attribute != null) { EventLog.Source = string.IsNullOrEmpty(attribute.EventLogSource) ? "WindowsServiceHarness" : attribute.EventLogSource; CanStop = attribute.CanStop; CanPauseAndContinue = attribute.CanPauseAndContinue; CanShutdown = attribute.CanShutdown; CanHandlePowerEvent = false; CanHandleSessionChangeEvent = false; AutoLog = true; } else { throw new InvalidOperationException( $"IWindowsService implementer {serviceImplementation.GetType().FullName} must have a WindowsServiceAttribute."); } }
// Run a service from the console given a service implementation public static void Run(string[] args, IWindowsService service) { string serviceName = service.GetType().Name; bool isRunning = true; // simulate starting the windows service service.OnStart(args); // let it run as long as Q is not pressed while (isRunning) { WriteToConsole(ConsoleColor.Yellow, "Enter either [Q]uit, [P]ause, [R]esume : "); isRunning = HandleConsoleInput(service, Console.ReadLine()); } // stop and shutdown service.OnStop(); service.OnShutdown(); }
/// <summary> /// Runs the service that implements <see cref="IWindowsService" />. /// </summary> /// <param name="args">The arguments.</param> /// <param name="service">The service.</param> public static void Run(string[] args, IWindowsService service) { var serviceName = service.GetType().Name; WriteToConsole(ConsoleColor.Green, serviceName); WriteToConsole(ConsoleColor.Yellow, "Enter either [Q]uit, [P]ause, [R]esume : "); var running = true; service.OnStart(args); while (running) { running = ReadConsoleInput(service, Console.ReadKey(true).Key.ToString()); } service.OnStop(); service.OnShutdown(); }
// Run a service from the console given a service implementation public static void Run(string[] args, IWindowsService service) { string serviceName = service.GetType().Name; bool isRunning = true; // simulate starting the windows service service.OnStart(args); // let it run as long as Q is not pressed while (isRunning) { WriteToConsole(ConsoleColor.Yellow, "MLS Marketing Menu [S + Return] para Sair : "); isRunning = HandleConsoleInput(service, Console.ReadLine()); } // stop and shutdown service.OnStop(); service.OnShutdown(); }
public static void Run(string[] args, IWindowsService service) { var serviceName = service.GetType().Name; service.WriteToConsole += OnWriteToConsole; var isRunning = true; AllocConsole(); service.Start(args); while (isRunning) { WriteToConsole(ConsoleColor.Yellow, "Enter either [Q]uit, [P]ause, [R]esume : "); isRunning = HandleConsoleInput(service, Console.ReadLine()); } service.Stop(); service.Shutdown(); }
private void ConfigureServiceFromAttributes(IWindowsService service) { var attribute = service.GetType().GetTypeInfo().GetCustomAttribute <WindowsServiceAttribute>(); if (attribute != null) { if (!string.IsNullOrWhiteSpace(attribute.EventLogSource)) { this.EventLog.Source = attribute.EventLogSource; } this.AutoLog = true; this.CanHandlePowerEvent = false; this.CanHandleSessionChangeEvent = false; this.CanPauseAndContinue = attribute.CanPauseAndContinue; this.CanShutdown = attribute.CanShutdown; this.CanStop = attribute.CanStop; } else { throw new InvalidOperationException( string.Format("IWindowsService implementer {0} must have a WindowsServiceAttribute.", service.GetType().FullName)); } }