private void ConfigureServiceFromAttributes(WindowsService service) { var attribute = service.GetType().GetAttribute<WindowsServiceAttribute>(); if (attribute != null) { EventLog.Source = string.IsNullOrEmpty(attribute.EventLogSource) ? "WindowsServiceHarness" : 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 ArgumentException( string.Format(Strings.EXCEPTION_ServiceMustBeMarkedWithAttribute, service.GetType().FullName)); } }
/// <summary> /// Runs a service from the console given a service implementation. /// </summary> /// <param name="args">The command line arguments to pass to the service.</param> /// <param name="service"> /// The <see cref="WindowsService" /> implementation to start. /// </param> public void Run(WindowsService service, string[] args) { var serviceName = service.ServiceName; var buildDate = Assembly.GetAssembly(service.GetType()).RetrieveLinkerTimestamp(); var isRunning = true; // Can't clear the console in a unit test, // so this line will throw an exception. Clear(); WriteLine(); WriteToConsole(ConsoleColor.White, Strings.DEBUG_ServiceNameAndBuildDate, serviceName, buildDate); WriteLine(); WriteToConsole(ConsoleColor.White, EndpointsHeader(service)); foreach (var endpoint in service.Endpoints) { WriteToConsole(ConsoleColor.White, Strings.DEBUG_EndpointHeader, endpoint); } // simulate starting the windows service service.OnStart(args); WriteLine(); WriteToConsole(ConsoleColor.White, Strings.DEBUG_ServiceStarted); // let it run as long as Q is not pressed while (isRunning) { WriteLine(); WriteToConsole(ConsoleColor.Yellow, Strings.DEBUG_EnterPauseResumeOrQuit); isRunning = HandleConsoleInput(service, ReadKey(true)); } // stop and shutdown WriteLine(); WriteToConsole(ConsoleColor.Yellow, Strings.DEBUG_ServiceStopping, serviceName); service.OnStop(); WriteToConsole(ConsoleColor.Yellow, Strings.DEBUG_ServiceStopped, serviceName); service.OnShutdown(); }