示例#1
0
        /// <summary>
        ///   When implemented in a derived class, executes when a Start command is sent to the service by the Service Control
        ///   Manager (SCM) or when the operating system starts (for a service that starts automatically). Specifies actions to
        ///   take when the service starts.
        /// </summary>
        /// <param name="args">Data passed by the Start command.</param>
        protected override void OnStart(string[] args)
        {
            _logger.InfoFormat("Starting {0} (v{1}) service.", ApplicationParameters.Name, ApplicationParameters.ProductVersion);

            try
            {
                //AutoMapperInitializer.Initialize();
                SimpleInjectorContainer.start();
                _container = SimpleInjectorContainer.Container;

                _subscription = EventManager.subscribe <ShutdownMessage>((message) => OnStop(), null, null);

                var fileSystem = _container.GetInstance <IFileSystem>();
                fileSystem.create_directory_if_not_exists(".\\files");
                fileSystem.create_directory_if_not_exists(".\\choco_logs");

                var tasks = _container.GetAllInstances <ITask>();
                foreach (var task in tasks)
                {
                    task.initialize();
                }

                _logger.InfoFormat("{0} service is now operational.", ApplicationParameters.Name);

                if ((args.Length > 0) && (Array.IndexOf(args, "/console") != -1))
                {
                    Console.WriteLine("Press Control+C to exit.");

                    // all of these options have their own issues.
                    //do
                    //{
                    //    Thread.Sleep(900000);
                    //}
                    //while (1==1);

                    // http://stackoverflow.com/a/16218470/18475
                    manualReset.WaitOne();

                    // this causes process calls (like calls to vagrant) to hang.
                    //Console.ReadKey();
                }
            }
            catch (Exception ex)
            {
                _logger.ErrorFormat(
                    "{0} service had an error on {1} (with user {2}):{3}{4}",
                    ApplicationParameters.Name,
                    Environment.MachineName,
                    Environment.UserName,
                    Environment.NewLine,
                    ex);
            }
        }