コード例 #1
0
ファイル: ServiceHarness.cs プロジェクト: LionFree/Cush
            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));
                }
            }
コード例 #2
0
ファイル: ConsoleHarness.cs プロジェクト: LionFree/Cush
        /// <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();
        }
コード例 #3
0
ファイル: ConsoleHarness.cs プロジェクト: LionFree/Cush
        private bool HandleConsoleInput(WindowsService service, ConsoleKeyInfo key)
        {
            var canContinue = true;

            // Check input
            switch (key.Key)
            {
                case ConsoleKey.Q:
                    WriteLine();
                    canContinue = false;
                    break;

                case ConsoleKey.P:
                    WriteLine();
                    WriteToConsole(ConsoleColor.Green, Strings.DEBUG_PausingService, service.ServiceName);
                    service.OnPause();
                    break;

                case ConsoleKey.R:
                    WriteLine();
                    WriteToConsole(ConsoleColor.Green,
                        Strings.DEBUG_ResumingService, service.ServiceName);
                    service.OnContinue();
                    break;

                default:
                    WriteToConsole(ConsoleColor.Red, Strings.DEBUG_BadKey);
                    break;
            }

            return canContinue;
        }
コード例 #4
0
ファイル: ConsoleHarness.cs プロジェクト: LionFree/Cush
 private string EndpointsHeader(WindowsService service)
 {
     return (service.Endpoints.Length == 1)
         ? Strings.DEBUG_EndpointHeaderSingular
         : (service.Endpoints.Length > 1)
             ? Strings.DEBUG_EndpointHeaderPlural
             : string.Empty;
 }
コード例 #5
0
ファイル: ServiceHarness.cs プロジェクト: LionFree/Cush
 internal ServiceHarnessImplementation([NotNull] WindowsService service)
 {
     _service = service;
     ConfigureServiceFromAttributes(_service);
 }
コード例 #6
0
ファイル: ServiceHarness.cs プロジェクト: LionFree/Cush
 internal static ServiceHarness WrapService(WindowsService service)
 {
     return new ServiceHarnessImplementation(service);
 }