예제 #1
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            //Set the working directory to the folder that this executable resides
            Environment.CurrentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            ILog logger = LogManager.GetLogger(typeof(Program));

#if DEBUG   //Run as a regular console application in Debug mode
            //Manually create an instance of SampleBackgroundService class and call its start method

            logger.Info("Starting services...");

            SampleBackgroundService _backgroundService = new SampleBackgroundService();
            _backgroundService.Start();

            logger.Info("Services started. Press enter to stop...");
            Console.ReadLine();

            logger.Info("Stopping service...");
            _backgroundService.Stop();
            logger.Info("Stopped.");
#else       //Create and run the real Windows service instance in Release mode
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[]
            {
                new SampleWindowsService()
            };
            ServiceBase.Run(ServicesToRun);
#endif
        }
예제 #2
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            //Set the working directory to the folder that this executable resides
            string exeName          = Assembly.GetExecutingAssembly().Location;
            string currentDirectory = Path.GetDirectoryName(exeName);

            Environment.CurrentDirectory = currentDirectory;

            string serviceName;
            string serviceDescription;

            serviceName        = Settings.Default.serviceName;
            serviceDescription = Settings.Default.serviceDescription;

            ILog logger = LogManager.GetLogger(typeof(Program));

            if (Environment.UserInteractive)
            {
                if (args.Length > 0)
                {
                    try
                    {
                        if (args[0].ToLower().Equals("/install"))
                        {
                            IDictionary       ht        = new Hashtable();
                            AssemblyInstaller installer = new AssemblyInstaller();
                            installer.Path          = exeName;
                            installer.UseNewContext = true;
                            installer.Install(ht);
                            installer.Commit(ht);
                            logger.Info(serviceName + " installed.");
                            Console.ReadKey();
                            return;
                        }
                        else if (args[0].ToLower().Equals("/uninstall"))
                        {
                            IDictionary       ht        = new Hashtable();
                            AssemblyInstaller installer = new AssemblyInstaller();
                            installer.Path          = exeName;
                            installer.UseNewContext = true;
                            installer.Uninstall(ht);
                            logger.Info(serviceName + " uninstalled.");
                            Console.ReadKey();
                            return;
                        }
                        else
                        {
                            Console.WriteLine("Valid arguments: /install and /uninstall");
                            Console.ReadKey();
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error("error installing service:", ex);
                        Console.ReadKey();
                        return;
                    }
                }

                logger.Info("Starting application...");

                SampleBackgroundService _backgroundService = new SampleBackgroundService();
                _backgroundService.Start();

                logger.Info("Application started. Press enter to stop...");
                Console.ReadLine();

                logger.Info("Stopping application...");
                _backgroundService.Stop();
                logger.Info("Stopped.");
            }
            else //Create and run the Windows service instance
            {
                logger.Info("Starting service...");
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[]
                {
                    new SampleWindowsService()
                };
                ServiceBase.Run(ServicesToRun);
                logger.Info("Service started");
            }
        }
예제 #3
0
 //Called when the Windows service is started
 protected override void OnStart(string[] args)
 {
     sampleBackgroundService = new SampleBackgroundService();
     sampleBackgroundService.Start();
 }