/// <summary>
        /// Install or initialize the service
        /// </summary>
        /// <returns>Return 0 is operation is succesfull, 1 otherwise</returns>
        public static int Main()
        {
            int retVal = 0;

            //Initialize the Event Log
            LoggerModule.InitLogger();
            LoggerModule.logger.Info(Localization.getResStr(modL10nData.EV_STARTING_ID));

            //It's used in install mode
            if (System.Environment.UserInteractive)
            {
                bool        bInstall            = false;
                bool        bUninstall          = false;
                enumAction  action              = enumAction.aUnknown;
                enumCommand search              = enumCommand.seUnknown;
                enumCommand searchInit          = enumCommand.seUnknown;
                int         timeoutMilliseconds = 30000;
                List <int>  listCommands        = new List <int>();

                // auto instala o desinstala el servicio windows
                foreach (string argument in (new Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase()).CommandLineArgs)
                {
                    switch (argument.ToLower())
                    {
                    case "-install":
                        action = enumAction.aInstall;
                        break;

                    case "-uninstall":
                        action = enumAction.aUninstall;
                        break;

                    case "-stop":
                        listCommands.Add((int)enumCommand.aStop);
                        break;

                    case "-start":
                        listCommands.Add((int)enumCommand.aStart);
                        break;

                    case "-restart":
                        listCommands.Add((int)enumCommand.aRestart);
                        break;

                    case "-initnone":
                        listCommands.Add((int)enumCommand.initNONE);
                        break;

                    case "-initall":
                        listCommands.Add((int)enumCommand.initALL);
                        break;

                    case "-initusb":
                        listCommands.Add((int)enumCommand.initUSB);
                        break;

                    case "-initeth":
                        listCommands.Add((int)enumCommand.initETH);
                        break;

                    case "-startusb":
                        listCommands.Add((int)enumCommand.startUSB);
                        break;

                    case "-starteth":
                        listCommands.Add((int)enumCommand.startETH);
                        break;

                    case "-stopusb":
                        listCommands.Add((int)enumCommand.stopUSB);
                        break;

                    case "-stopeth":
                        listCommands.Add((int)enumCommand.stopETH);
                        break;
                    }
                }

                switch (action)
                {
                case enumAction.aInstall:
                    try
                    {
                        // install
                        ManagedInstallerClass.InstallHelper(new string[] { System.Reflection.Assembly.GetExecutingAssembly().Location });
                    }
                    catch (Exception ex)
                    {
                        LoggerModule.logger.Error("Installing service. " + ex.Message);
                        retVal = 1;
                    }
                    break;

                case enumAction.aUninstall:
                    try
                    {
                        // uninstall
                        ManagedInstallerClass.InstallHelper(new string[] { "/u", System.Reflection.Assembly.GetExecutingAssembly().Location });
                    }
                    catch (Exception ex)
                    {
                        LoggerModule.logger.Error("Uninstalling service. " + ex.Message);
                        retVal = 1;
                    }
                    break;

                default:
                    ServiceController service = new ServiceController(WINDOWS_SERVICE_NAME);

                    if (service != null)
                    {
                        // it is installed
                        TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

                        foreach (enumCommand comm in listCommands)
                        {
                            switch (comm)
                            {
                            case enumCommand.aStart:
                                try
                                {
                                    service.Start();
                                    service.WaitForStatus(ServiceControllerStatus.Running, timeout);
                                }
                                catch (Exception ex)
                                {
                                    LoggerModule.logger.Error("Starting service. " + ex.Message);
                                    retVal = 1;
                                }
                                break;

                            case enumCommand.aStop:
                                try
                                {
                                    service.Stop();
                                    service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
                                }
                                catch (Exception ex)
                                {
                                    LoggerModule.logger.Error("Stopping service. " + ex.Message);
                                    retVal = 1;
                                }
                                break;

                            case enumCommand.aRestart:
                                try
                                {
                                    if (service.Status == ServiceControllerStatus.Running)
                                    {
                                        service.Stop();
                                        service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
                                    }
                                    service.Start();
                                    service.WaitForStatus(ServiceControllerStatus.Running, timeout);
                                }
                                catch (Exception ex)
                                {
                                    LoggerModule.logger.Error("Restarting service. " + ex.Message);
                                    retVal = 1;
                                }
                                break;

                            case enumCommand.startUSB:
                            case enumCommand.startETH:
                            case enumCommand.stopUSB:
                            case enumCommand.stopETH:
                                try
                                {
                                    // should be started for custom commands
                                    service.WaitForStatus(ServiceControllerStatus.Running, timeout);
                                    service.ExecuteCommand((System.Int32)comm);
                                }
                                catch (Exception ex)
                                {
                                    LoggerModule.logger.Error(string.Format("Error trying to custom command {0}: " + ex.Message, comm.ToString()));
                                    retVal = 1;
                                }
                                break;

                            case enumCommand.initALL:
                            case enumCommand.initETH:
                            case enumCommand.initUSB:
                            case enumCommand.initNONE:
                                try
                                {
                                    // should be started for custom commands
                                    service.WaitForStatus(ServiceControllerStatus.Running, timeout);
                                    service.ExecuteCommand((System.Int32)comm);
                                }
                                catch (Exception ex)
                                {
                                    LoggerModule.logger.Error(string.Format("Error trying to custom command {0}: " + ex.Message, comm.ToString()));
                                    retVal = 1;
                                }
                                break;
                            }
                        }
                    }

                    //Poder ejecutar el servicio sin necesidad de instalarlo
#if DEBUG_TRACECONTROLLOCAL && DEBUG
                    My.Settings.Default.SearchUSB = true;
                    My.Settings.Default.SearchETH = true;

                    string[] args = new string[1];
                    JBCTraceControlLocalSrv serv = new JBCTraceControlLocalSrv();
                    serv.OnStart(args);
                    while (true)
                    {
                        Console.Read();
                    }
                    serv.OnStop();
#endif
                    break;
                }
            }
            else
            {
                // crea el servicio windows
                ServiceBase.Run(new JBCTraceControlLocalSrv());
            }

            return(retVal);
        }
Esempio n. 2
0
        public ActionResult Index(StudentModel model, string order)
        {
            ServiceReference.StudentWebServiceClient sc = new ServiceReference.StudentWebServiceClient();
            enumCommand command = (enumCommand)Enum.Parse(typeof(enumCommand), order);

            if (command == enumCommand.Add)
            {
                ServiceReference.Student newStudent;
                ServiceReference.Student student = new ServiceReference.Student
                {
                    StudentId = model.StudentID,
                    Name      = model.Name,
                    Surname   = model.Surname,
                    Age       = model.Age
                };
                newStudent = sc.Add(student);
                if (newStudent != null)
                {
                    model.Resutl = WebConfigurationManager.AppSettings["Add"];
                }
                else
                {
                    model.Resutl = WebConfigurationManager.AppSettings["Error"];
                }
            }

            if (command == enumCommand.Update)
            {
                ServiceReference.Student newStudent;
                SkySales.Presentation.Web.ServiceReference.Student student = new ServiceReference.Student
                {
                    StudentId = model.StudentID,
                    Name      = model.Name,
                    Surname   = model.Surname,
                    Age       = model.Age
                };
                newStudent = sc.Update(student);
                if (newStudent != null)
                {
                    model.Resutl = WebConfigurationManager.AppSettings["Update"];
                }
                else
                {
                    model.Resutl = WebConfigurationManager.AppSettings["Error"];
                }
            }

            if (command == enumCommand.Delete)
            {
                ServiceReference.Student student;
                student = sc.Delete(model.StudentID);
                if (student != null)
                {
                    model.Resutl = WebConfigurationManager.AppSettings["Delete"];
                }
                else
                {
                    model.Resutl = WebConfigurationManager.AppSettings["Error"];
                }
            }

            if (command == enumCommand.GetById)
            {
                SkySales.Presentation.Web.ServiceReference.Student student = sc.GetById(model.StudentID);
                if (student != null)
                {
                    model.Resutl = WebConfigurationManager.AppSettings["GetById"];
                    model        = student;
                }
                else
                {
                    model.Resutl = WebConfigurationManager.AppSettings["Error"];
                }
            }

            if (command == enumCommand.GetAll)
            {
                ServiceReference.Student[] students;
                students = sc.GetAll();
                ListBox listBox = new ListBox();
            }

            return(View(model));
        }