示例#1
0
        protected override void OnStart(string[] args)
        {
            try
            {
                var section = TeamLabSvcConfigurationSection.GetSection();
                foreach (TeamLabSvcConfigurationElement e in section.TeamlabServices)
                {
                    if (!e.Disable)
                    {
                        services.Add((IServiceController)Activator.CreateInstance(Type.GetType(e.Type, true)));
                    }
                    else
                    {
                        log.InfoFormat("Skip service {0}", e.Type);
                    }
                }
            }
            catch (Exception error)
            {
                log.ErrorFormat("Can not start services: {0}", error);
                return;
            }

            foreach (var s in services)
            {
                try
                {
                    s.Start();
                    log.InfoFormat("Service {0} started.", GetServiceName(s));
                }
                catch (Exception error)
                {
                    log.ErrorFormat("Can not start service {0}: {1}", GetServiceName(s), error);
                }
            }
        }
示例#2
0
        protected override void OnStart(string[] args)
        {
            try
            {
                // start all services from config or start only one service from parameter -t ServiceType
                if (args.Length == 0)
                {
                    args = Environment.GetCommandLineArgs();
                }

                var serviceType = string.Empty;
                for (var i = 0; i < args.Length; i++)
                {
                    if (args[i] == "-s" || args[i] == "--service")
                    {
                        if (string.IsNullOrEmpty(args[i + 1]))
                        {
                            throw new ArgumentNullException("--service", "Type of service not found.");
                        }
                        serviceType = args[i + 1].Trim().Trim('"');
                    }
                }

                if (!string.IsNullOrEmpty(serviceType))
                {
                    services.Add((IServiceController)Activator.CreateInstance(Type.GetType(serviceType, true)));
                }
                else
                {
                    var section = TeamLabSvcConfigurationSection.GetSection();
                    if (section != null)
                    {
                        foreach (TeamLabSvcConfigurationElement e in section.TeamlabServices)
                        {
                            services.Add((IServiceController)Activator.CreateInstance(Type.GetType(e.Type, true)));
                        }
                    }
                }

                if (!services.Any())
                {
                    throw new InvalidOperationException("No services to start.");
                }
            }
            catch (Exception error)
            {
                log.ErrorFormat("Can not start services: {0}", error);
                return;
            }

            foreach (var s in services)
            {
                try
                {
                    s.Start();
                    log.InfoFormat("Service {0} started.", GetServiceName(s));
                }
                catch (Exception error)
                {
                    log.ErrorFormat("Can not start service {0}: {1}", GetServiceName(s), error);
                }
            }
        }