Exemplo n.º 1
0
        private static void RunService(CommandLineParser parser, Action <StringDictionary> environment, ILog logger)
        {
            if (ServiceEnvironmentManagementEx.IsServiceDisabled(parser.Target))
            {
                logger.ErrorFormat("The service '{0}' is disabled. Please enable the service.",
                                   parser.Target);
                return;
            }

            var service = new ServiceController(parser.Target);

            if (service.Status != ServiceControllerStatus.Stopped)
            {
                logger.ErrorFormat("The service '{0}' is already running. The profiler cannot attach to an already running service.",
                                   parser.Target);
                return;
            }

            // now to set the environment variables
            var profilerEnvironment = new StringDictionary();

            environment(profilerEnvironment);

            var serviceEnvironment = new ServiceEnvironmentManagement();

            try
            {
                serviceEnvironment.PrepareServiceEnvironment(parser.Target,
                                                             (from string key in profilerEnvironment.Keys select string.Format("{0}={1}", key, profilerEnvironment[key])).ToArray());

                // now start the service
                service = new ServiceController(parser.Target);
                service.Start();
                logger.InfoFormat("Service starting '{0}'", parser.Target);
                service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
                logger.InfoFormat("Service started '{0}'", parser.Target);
            }
            finally
            {
                // once the serice has started set the environment variables back - just in case
                serviceEnvironment.ResetServiceEnvironment();
            }

            // and wait for it to stop
            service.WaitForStatus(ServiceControllerStatus.Stopped);
            logger.InfoFormat("Service stopped '{0}'", parser.Target);
        }
Exemplo n.º 2
0
        private static void RunService(CommandLineParser parser, Action <StringDictionary> environment, ILog logger)
        {
            if (ServiceEnvironmentManagementEx.IsServiceDisabled(parser.Target))
            {
                logger.ErrorFormat("The service '{0}' is disabled. Please enable the service.",
                                   parser.Target);
                return;
            }

            var service = new ServiceController(parser.Target);

            try
            {
                if (service.Status != ServiceControllerStatus.Stopped)
                {
                    logger.ErrorFormat(
                        "The service '{0}' is already running. The profiler cannot attach to an already running service.",
                        parser.Target);
                    return;
                }

                // now to set the environment variables
                var profilerEnvironment = new StringDictionary();
                environment(profilerEnvironment);

                var serviceEnvironment = new ServiceEnvironmentManagement();

                try
                {
                    serviceEnvironment.PrepareServiceEnvironment(
                        parser.Target,
                        parser.ServiceEnvironment,
                        (from string key in profilerEnvironment.Keys
                         select string.Format("{0}={1}", key, profilerEnvironment[key])).ToArray());

                    // now start the service
                    var old = service;
                    service = new ServiceController(parser.Target);
                    old.Dispose();

                    if (parser.Target.ToLower().Equals("w3svc"))
                    {
                        // Service will not automatically start
                        if (!TerminateCurrentW3SvcHost(logger) ||
                            !ServiceEnvironmentManagementEx.IsServiceStartAutomatic(parser.Target))
                        {
                            service.Start();
                        }
                    }
                    else
                    {
                        service.Start();
                    }
                    logger.InfoFormat("Service starting '{0}'", parser.Target);
                    service.WaitForStatus(ServiceControllerStatus.Running, parser.ServiceStartTimeout);
                    logger.InfoFormat("Service started '{0}'", parser.Target);
                }
                catch (InvalidOperationException fault)
                {
                    logger.FatalFormat("Service launch failed with '{0}'", fault);
                }
                finally
                {
                    // once the serice has started set the environment variables back - just in case
                    serviceEnvironment.ResetServiceEnvironment();
                }

                // and wait for it to stop
                service.WaitForStatus(ServiceControllerStatus.Stopped);
                logger.InfoFormat("Service stopped '{0}'", parser.Target);

                // Stopping w3svc host
                if (parser.Target.ToLower().Equals("w3svc"))
                {
                    logger.InfoFormat("Stopping svchost to clean up environment variables for w3svc", parser.Target);
                    if (ServiceEnvironmentManagementEx.IsServiceStartAutomatic(parser.Target))
                    {
                        logger.InfoFormat("Please note that the 'w3svc' service may automatically start");
                    }
                    TerminateCurrentW3SvcHost(logger);
                }
            }
            finally
            {
                service.Dispose();
            }
        }