Esempio n. 1
0
        public static void Run <T>(string serviceName = "StandardService") where T : IService, new()
        {
            try
            {
                if (Environment.UserInteractive)
                {
                    var cmd = Environment.GetCommandLineArgs()
                              .Skip(1)
                              .FirstOrDefault()
                              ?.ToLower();
                    switch (cmd)
                    {
                    case "pai":
                        BasicServiceInstaller.Install(serviceName);
                        ServiceController service = new ServiceController(serviceName);
                        service.Start();
                        service.WaitForStatus(ServiceControllerStatus.Running);
                        using (var s = new T())
                        {
                            s.Start(Environment.GetCommandLineArgs());
                        }
                        Console.WriteLine("Done");
                        break;

                    case "pa-u":
                        BasicServiceInstaller.Uninstall(serviceName);
                        break;

                    default:
                        using (var s = new T())
                        {
                            s.Start(Environment.GetCommandLineArgs());
                        }
                        break;
                    }
                }
                else
                {
                    ServiceBase.Run(new BasicService <T> {
                        ServiceName = serviceName
                    });
                }
            }
            catch (Exception ex)
            {
                LoggerManager.Instance.Error(ex);
            }
        }
        public static void Run <T>(string serviceName = "StandardService") where T : IService, new()
        {
            try
            {
                AppDomain.CurrentDomain.UnhandledException += (s, e) =>
                {
                    if (EventLog.SourceExists(serviceName))
                    {
                        EventLog.WriteEntry(serviceName,
                                            "Fatal Exception : " + Environment.NewLine +
                                            e.ExceptionObject, EventLogEntryType.Error);
                    }
                };

                if (Environment.UserInteractive)
                {
                    var cmd = Environment.GetCommandLineArgs()
                              .Skip(1)
                              .FirstOrDefault()
                              .vnSafe(string.Empty)
                              .ToLower();
                    switch (cmd)
                    {
                    case "pai":
                        BasicServiceInstaller.Install(serviceName);
                        ServiceController service = new ServiceController(serviceName);
                        service.Start();
                        service.WaitForStatus(ServiceControllerStatus.Running);
                        using (var s = new T())
                        {
                            s.DefaultStart();
                        }
                        break;

                    case "pa-u":
                        BasicServiceInstaller.Uninstall(serviceName);
                        break;

                    default:
                        using (var s = new T())
                        {
                            s.DefaultStart();
                            s.Dispose();
                        }
                        break;
                    }
                }
                else
                {
                    ServiceBase.Run(new BasicService <T> {
                        ServiceName = serviceName
                    });
                }
            }
            catch (Exception ex)
            {
                using (var w = new StreamWriter("ZeroEye.log"))
                {
                    w.WriteLine($"{DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss")} : {ex.Message}");
                }
                Environment.Exit(1);
            }
        }