예제 #1
0
        public ChoServiceInstaller()
        {
            ChoService.ServiceInstallerInitialization(this, new ChoServiceInstallerEventArgs(ChoServiceProcessInstallerSettings.Me, ChoServiceInstallerSettings.Me));
            ServiceProcessInstaller processInstaller = new ServiceProcessInstaller();

            ChoApplication.WriteToEventLog(ChoServiceProcessInstallerSettings.Me.ToString());
            processInstaller.Account = ChoServiceProcessInstallerSettings.Me.Account;
            if (ChoServiceProcessInstallerSettings.Me.Account == ServiceAccount.User)
            {
                processInstaller.Username = ChoServiceProcessInstallerSettings.Me.UserName;
                processInstaller.Password = ChoServiceProcessInstallerSettings.Me.Password;
            }

            ChoApplication.WriteToEventLog(ChoServiceInstallerSettings.Me.ToString());
            ServiceInstaller serviceInstaller = new ServiceInstaller();

            serviceInstaller.DisplayName      = ChoServiceInstallerSettings.Me.DisplayName;
            serviceInstaller.StartType        = ChoServiceInstallerSettings.Me.ServiceStartMode;
            serviceInstaller.ServiceName      = ChoServiceInstallerSettings.Me.ServiceName;
            serviceInstaller.DelayedAutoStart = ChoServiceInstallerSettings.Me.DelayedAutoStart;
            serviceInstaller.Description      = ChoServiceInstallerSettings.Me.Description;

            this.Installers.Add(processInstaller);
            this.Installers.Add(serviceInstaller);
        }
예제 #2
0
        internal static void Initialize(ChoApplicationHost host)
        {
            if (!ChoApplicationHost.IsApplicationHostUsed)
            {
                return;
            }

            if (!Environment.UserInteractive)
            {
                //Debugger.Break();
                ChoService service = new ChoService(host);

                service.CanHandlePowerEvent         = ChoServiceInstallerSettings.Me.CanHandlePowerEvent;
                service.CanHandleSessionChangeEvent = ChoServiceInstallerSettings.Me.CanHandleSessionChangeEvent;
                service.CanPauseAndContinue         = ChoServiceInstallerSettings.Me.CanPauseAndContinue;
                service.CanShutdown = ChoServiceInstallerSettings.Me.CanShutdown;
                service.CanStop     = ChoServiceInstallerSettings.Me.CanStop;
                service.AutoLog     = ChoServiceInstallerSettings.Me.AutoLog;
                //service.ExitCode = ChoServiceInstallerSettings.Me.ExitCode;

                //Windows Service Mode
                ServiceBase[] ServicesToRun = new ServiceBase[]
                {
                    service
                };
                ServiceBase.Run(ServicesToRun);
            }
            else
            {
                //Parse command line arguments, install,
                //try
                //{
                ChoFrameworkCmdLineArgs frameworkCmdLineArgs = new ChoFrameworkCmdLineArgs();
                frameworkCmdLineArgs.Init();
                ChoServiceCommandLineArgs serviceCmdLineArgs = new ChoServiceCommandLineArgs();
                serviceCmdLineArgs.Init();

                ServiceController sc = new ServiceController(ChoServiceCommandLineArgs.GetServiceName(), Environment.MachineName);
                if (serviceCmdLineArgs.InstallService)
                {
                    ChoManagedInstallerClass.InstallService();

                    //Save the command line parameters
                    //if (serviceCmdLineArgs.ServiceParams != null)
                    //{
                    //    try
                    //    {
                    //        ChoRegistryKey _rkAppRun = new ChoRegistryKey(String.Format(RegRunSubKey.FormatString(ChoApplication.Host.ServiceName), true));
                    //        _rkAppRun.SetValue("ImagePath", "{0} {1}".FormatString(ChoAssembly.GetEntryAssembly().Location, ChoServiceCommandLineArgs.GetServiceParams().Replace("'", @"""")));
                    //    }
                    //    catch (Exception ex)
                    //    {
                    //        System.Diagnostics.Trace.TraceError(ex.ToString());
                    //    }
                    //}
                }
                else if (serviceCmdLineArgs.UninstallService)
                {
                    ChoManagedInstallerClass.UninstallService();
                }
                else if (serviceCmdLineArgs.StartService)
                {
                    if (!ChoWindowsIdentity.IsAdministrator())
                    {
                        ChoApplication.RestartAsAdmin();
                        return;
                    }

                    if (serviceCmdLineArgs.ServiceParams == null ||
                        serviceCmdLineArgs.ServiceParams.Length == 0)
                    {
                        //ChoEnvironment.CommandLineArgs = Environment.GetCommandLineArgs().Skip(1).ToArray();
                    }
                    else
                    {
                        string commandLineArgs = null;
                        if (serviceCmdLineArgs.ServiceParams.StartsWith("\"") &&
                            serviceCmdLineArgs.ServiceParams.EndsWith("\""))
                        {
                            commandLineArgs = serviceCmdLineArgs.ServiceParams.Substring(1, serviceCmdLineArgs.ServiceParams.Length - 2);
                        }
                        else
                        {
                            commandLineArgs = serviceCmdLineArgs.ServiceParams;
                        }

                        ChoEnvironment.CommandLineArgs = commandLineArgs.SplitNTrim(' ');
                    }

                    sc.Start(ChoEnvironment.CommandLineArgs);

                    if (ChoServiceInstallerSettings.Me.Timeout > 0)
                    {
                        sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(ChoServiceInstallerSettings.Me.Timeout));
                    }
                }
                else if (serviceCmdLineArgs.StopService)
                {
                    if (!ChoWindowsIdentity.IsAdministrator())
                    {
                        ChoApplication.RestartAsAdmin();
                        return;
                    }
                    sc.Stop();
                }
                else if (serviceCmdLineArgs.PauseService)
                {
                    if (!ChoWindowsIdentity.IsAdministrator())
                    {
                        ChoApplication.RestartAsAdmin();
                        return;
                    }
                    sc.Pause();
                }
                else if (serviceCmdLineArgs.ContinueService)
                {
                    if (!ChoWindowsIdentity.IsAdministrator())
                    {
                        ChoApplication.RestartAsAdmin();
                        return;
                    }
                    sc.Continue();
                }
                else if (serviceCmdLineArgs.ExecuteCommand != Int32.MinValue)
                {
                    if (!ChoWindowsIdentity.IsAdministrator())
                    {
                        ChoApplication.RestartAsAdmin();
                        return;
                    }
                    sc.ExecuteCommand(serviceCmdLineArgs.ExecuteCommand);
                }
                else
                {
                    //ChoApplicationMode? applicationMode = ChoFrameworkCmdLineArgs.GetApplicationMode();
                    //if (applicationMode != null)
                    //    ChoApplication.ApplicationMode = applicationMode.Value;

                    //ChoProfile.WriteLine(ChoApplication.ApplicationMode.ToString());
                    if (ChoApplication.ApplicationMode == ChoApplicationMode.Windows)
                    {
                        ChoApplication.Host.OnStartService(ChoEnvironment.CommandLineArgs);
                    }
                    else if (ChoApplication.ApplicationMode == ChoApplicationMode.Console)
                    {
                        if (ChoConsoleSettings.Me.ConsoleMode != uint.MinValue && ChoWindowsManager.ConsoleWindowHandle != IntPtr.Zero)
                        {
                            ChoKernel32.SetConsoleMode(ChoWindowsManager.ConsoleWindowHandle, (uint)ChoConsoleSettings.Me.ConsoleMode);
                        }

                        ChoApplicationHost.RegisterConsoleControlHandler();
                        ChoApplication.Host.OnStartService(ChoEnvironment.CommandLineArgs);
                        //ChoApplication.Host.OnStopService();
                    }
                    else if (ChoApplication.ApplicationMode == ChoApplicationMode.Web)
                    {
                        ChoApplication.Host.OnStartService(ChoEnvironment.CommandLineArgs);
                    }
                }
                //}
                //catch (ChoFatalApplicationException)
                //{
                //    throw;
                //}
                //catch (ChoCommandLineArgException argEx)
                //{
                //    ChoApplication.DisplayMsg(argEx.Message);
                //    throw;
                //}
                //catch (ChoCommandLineArgUsageException usageEx)
                //{
                //    ChoApplication.DisplayMsg(usageEx.Message);
                //    throw;
                //}
                //catch (Exception ex)
                //{
                //    ChoApplication.DisplayMsg(ex.Message, ex);
                //    throw;
                //}
            }
        }