Ejemplo n.º 1
0
        private void MainForm_Load(object sender, EventArgs e)
        {
            this.Text = Program.SVC_NAME;

            CommonLog.SetGuiControl(this, mmLog);

            RemoteCaller.HttpConnectionLimit = 1000; // by default

            var appSettings = ConfigurationManager.AppSettings;

            var allKeys = appSettings.AllKeys;

            foreach (var key in allKeys)
            {
                if (key == "OutgoingHttpConnectionLimit")
                {
                    RemoteCaller.HttpConnectionLimit = Convert.ToInt32(appSettings[key].ToString());
                }

                if (key == "DefaultRemoteCallTimeout")
                {
                    RemoteCaller.DefaultTimeout = Convert.ToInt32(appSettings[key].ToString());
                }

                if (key == "AppServerSetting")
                {
                    m_ServerSetting = JsonConvert.DeserializeObject <CommonServerContainerSetting>(appSettings[key]);
                }
            }

            if (m_Server == null && m_ServerSetting != null)
            {
                m_Server = new CommonServerContainer();
            }
        }
Ejemplo n.º 2
0
 private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
 {
     if (m_Server != null && m_Server.IsWorking())
     {
         if (!btnStop.Enabled)
         {
             e.Cancel = true;
         }
         else
         {
             btnStop.Enabled = false;
             CommonLog.Info("Exiting...");
             this.Text = this.Text + " - Exiting...";
             if (m_Server != null)
             {
                 m_Server.Stop();
             }
             e.Cancel = false;
         }
     }
     else
     {
         e.Cancel = false;
     }
 }
Ejemplo n.º 3
0
        private async void btnStart_Click(object sender, EventArgs e)
        {
            if (m_Server != null && !m_Server.IsWorking() && m_ServerSetting != null)
            {
                btnStart.Enabled = false;
                CommonLog.Info("Starting...");
                await m_Server.StartAsync(m_ServerSetting, CommonLog.GetLogger());

                btnStop.Enabled = true;
            }
        }
        protected override void OnStop()
        {
            // TODO: Add code here to perform any tear-down necessary to stop your service.

            RequestAdditionalTime(1000 * Program.EXTRA_STOP_SVC_SECONDS);

            if (m_Server != null && m_Server.IsWorking())
            {
                m_Server.Stop();
                Thread.Sleep(100);
            }

            CommonLog.Info("=== " + Program.SVC_NAME + " stopped ===");
        }
        protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.

            RequestAdditionalTime(1000 * Program.EXTRA_START_SVC_SECONDS);

            RemoteCaller.HttpConnectionLimit = 1000; // by default

            CommonLog.Info("=== " + Program.SVC_NAME + " is starting ===");

            ConfigurationManager.RefreshSection("appSettings");

            var appSettings = ConfigurationManager.AppSettings;

            var allKeys = appSettings.AllKeys;

            foreach (var key in allKeys)
            {
                if (key == "OutgoingHttpConnectionLimit")
                {
                    RemoteCaller.HttpConnectionLimit = Convert.ToInt32(appSettings[key].ToString());
                }

                if (key == "DefaultRemoteCallTimeout")
                {
                    RemoteCaller.DefaultTimeout = Convert.ToInt32(appSettings[key].ToString());
                }

                if (key == "AppServerSetting")
                {
                    m_ServerSetting = JsonConvert.DeserializeObject <CommonServerContainerSetting>(appSettings[key]);
                }
            }

            if (m_Server == null && m_ServerSetting != null)
            {
                m_Server = new CommonServerContainer();
            }

            if (m_Server != null && !m_Server.IsWorking() && m_ServerSetting != null)
            {
                m_Server.Start(m_ServerSetting, CommonLog.GetLogger());
                Thread.Sleep(100);
            }
        }
Ejemplo n.º 6
0
        public static int Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException;

            var appSettings = ConfigurationManager.AppSettings;
            var allKeys     = appSettings.AllKeys;

            foreach (var key in allKeys)
            {
                if (key == "MuteVarName")
                {
                    MUTE_VAR = appSettings[key];
                }
                if (key == "ServiceName")
                {
                    SVC_NAME = appSettings[key];
                }
                if (key == "ServiceDescription")
                {
                    SVC_DESC = appSettings[key];
                }

                if (key == "ExtraStartServiceSeconds")
                {
                    EXTRA_START_SVC_SECONDS = Convert.ToInt32(appSettings[key].ToString());
                }
                if (key == "ExtraStopServiceSeconds")
                {
                    EXTRA_STOP_SVC_SECONDS = Convert.ToInt32(appSettings[key].ToString());
                }
            }

            // start the program ...

            if (Environment.UserInteractive)
            {
                string parameter   = "";
                bool   needDetails = false;

                if (args != null && args.Length >= 1)
                {
                    parameter = args[0];
                    parameter = parameter.Trim();

                    if (parameter == "install" || parameter == "uninstall" ||
                        parameter == "silent-install" || parameter == "silent-uninstall")
                    {
                        if (parameter == "silent-install" || parameter == "silent-uninstall")
                        {
                            if (parameter == "silent-install")
                            {
                                parameter = "install";
                            }
                            else if (parameter == "silent-uninstall")
                            {
                                parameter = "uninstall";
                            }
                        }
                        else
                        {
                            // redirect console output to parent process, normally it should be "cmd"
                            AttachConsole(ATTACH_PARENT_PROCESS);
                            needDetails = true;
                        }

                        string svcName = "";

                        if (args.Length >= 2)
                        {
                            string[] svcNameArgs = new string[args.Length - 1];
                            for (int i = 1; i < args.Length; i++)
                            {
                                svcNameArgs[i - 1] = args[i];
                            }

                            for (int i = 0; i < svcNameArgs.Length; i++)
                            {
                                if (svcName.Length == 0)
                                {
                                    svcName = svcNameArgs[i];
                                }
                                else
                                {
                                    svcName = svcName + " " + svcNameArgs[i];
                                }
                            }

                            svcName = svcName.Trim();
                        }

                        if (svcName.Length > 0)
                        {
                            SVC_NAME = svcName;
                        }
                    }
                    else
                    {
                        parameter = "";
                    }
                }

                parameter = parameter.Trim();
                if (parameter == "install" && SVC_NAME.Length > 0)
                {
                    Console.WriteLine("Start to install service with name [" + SVC_NAME + "]");
                    CommonLog.Info("Installing service...");
                    try
                    {
                        ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
                        CommonLog.Info("OK");
                        Console.WriteLine("Installed service [" + SVC_NAME + "] successfully");
                        if (needDetails)
                        {
                            Console.WriteLine("You might need to press enter to end the process");
                        }
                    }
                    catch (Exception ex)
                    {
                        CommonLog.Error("Error: " + ex.Message);
                        Console.WriteLine("Failed to install service [" + SVC_NAME + "]");
                        if (needDetails)
                        {
                            Console.WriteLine("You might need to press enter to end the process");
                        }
                        return(-1);
                    }
                }
                else if (parameter == "uninstall" && SVC_NAME.Length > 0)
                {
                    Console.WriteLine("Start to uninstall service [" + SVC_NAME + "]");
                    CommonLog.Info("Uninstalling service...");
                    try
                    {
                        ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
                        CommonLog.Info("OK");
                        Console.WriteLine("Uninstalled service [" + SVC_NAME + "] successfully");
                        if (needDetails)
                        {
                            Console.WriteLine("You might need to press enter to end the process");
                        }
                    }
                    catch (Exception ex)
                    {
                        CommonLog.Error("Error: " + ex.Message);
                        //Console.WriteLine("Failed to uninstall service [" + SVC_NAME + "]");
                        if (needDetails)
                        {
                            Console.WriteLine("You might need to press enter to end the process");
                        }
                        return(-1);
                    }
                }
                else // Run as a console app normally ...
                {
                    bool canRun = false;
                    mutex = new Mutex(true, MUTE_VAR + ".Instance", out canRun);

                    if (!canRun)
                    {
                        MessageBox.Show("Another application instance is already running.");
                    }
                    else
                    {
                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);
                        Application.Run(new MainForm());
                        Environment.Exit(0);
                    }
                }
            }
            else
            {
                bool canRun = false;
                mutex = new Mutex(true, MUTE_VAR + ".Instance", out canRun);

                if (!canRun)
                {
                    throw new Exception("Another application instance is already running.");
                }
                else
                {
                    ServiceBase[] ServicesToRun;
                    ServicesToRun = new ServiceBase[]
                    {
                        new SimpleSharpServerService()
                    };
                    ServiceBase.Run(ServicesToRun);
                }
            }

            return(0);
        }
Ejemplo n.º 7
0
 private static void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
 {
     CommonLog.Error(((Exception)e.ExceptionObject).Message + ((Exception)e.ExceptionObject).InnerException.Message);
 }
Ejemplo n.º 8
0
        static void Main(string[] args)
        {
            m_AppName = "SimpleSharpServer";

            m_ExtraStopAppSeconds = 2;

            m_Watcher = new ManualResetEvent(false);

            AppDomain.CurrentDomain.ProcessExit += (s, ev) =>
            {
                Console.WriteLine(m_AppName + " - process exit");
                if (m_Watcher != null)
                {
                    m_Watcher.Set();
                }
            };

            Console.CancelKeyPress += (s, ev) =>
            {
                Console.WriteLine(m_AppName + " - Ctrl+C pressed");
                if (m_Watcher != null)
                {
                    m_Watcher.Set();
                }
                ev.Cancel = true;
            };

            LogManager.Configuration = new XmlLoggingConfiguration($"{AppContext.BaseDirectory}/NLog.config");

            RemoteCaller.HttpConnectionLimit = 1000; // by default

            var appSettings = ConfigurationManager.AppSettings;
            var allKeys     = appSettings.AllKeys;

            foreach (var key in allKeys)
            {
                if (key == "ServiceName")
                {
                    m_AppName = appSettings[key];
                }
                if (key == "ExtraStopServiceSeconds")
                {
                    m_ExtraStopAppSeconds = Convert.ToInt32(appSettings[key].ToString());
                }
                if (key == "OutgoingHttpConnectionLimit")
                {
                    RemoteCaller.HttpConnectionLimit = Convert.ToInt32(appSettings[key].ToString());
                }
                if (key == "DefaultRemoteCallTimeout")
                {
                    RemoteCaller.DefaultTimeout = Convert.ToInt32(appSettings[key].ToString());
                }
                if (key == "AppServerSetting")
                {
                    m_ServerSetting = JsonConvert.DeserializeObject <CommonServerContainerSetting>(appSettings[key]);
                }
            }

            if (m_ServerSetting == null)
            {
                CommonLog.Info("Invalid app config");
                return;
            }

            CommonLog.Info("=== " + m_AppName + " is starting ===");

            if (m_Server == null && m_ServerSetting != null)
            {
                m_Server = new CommonServerContainer();
            }
            if (m_Server != null && !m_Server.IsWorking() && m_ServerSetting != null)
            {
                m_Server.Start(m_ServerSetting, CommonLog.GetLogger());
                Thread.Sleep(100);
            }

            if (m_Watcher != null)
            {
                m_Watcher.Reset();
                m_Watcher.WaitOne();
            }

            if (m_Server != null && m_Server.IsWorking())
            {
                m_Server.Stop();
                Thread.Sleep(100);
            }

            CommonLog.Info("=== " + m_AppName + " stopped ===");

            Thread.Sleep(1000 * m_ExtraStopAppSeconds);

            //Console.WriteLine();
            //Console.WriteLine("Press any key to quit...");
            //Console.ReadLine();
        }