/// <summary> /// This method is used to actually install the service. The service is installed with the /// specified dependency included. If the dependency parameter is null, no dependency is /// created. /// </summary> /// <param name="customInstaller">The installer object.</param> /// <param name="dependencies">The specified dependency.</param> static void Install(CustomServiceInstaller customInstaller, string dependencies) { // Make sure that the service does not already exist as an installed service //if (customInstaller.DoesServiceExists(WindowsServiceConstants.SERVICE_NAME) == false) if (customInstaller.DoesServiceExists(ServiceName) == false) { // Build the path to the executable var sServiceExe = Environment.CurrentDirectory + "\\" + WindowsServiceConstants.SERVICE_EXECUTABLE; // Attempts to install the service. var bSuccess = customInstaller.InstallService(sServiceExe, ServiceName, DisplayName, ServiceDescription, CustomServiceInstaller.SERVICE_START_TYPE.SERVICE_AUTO_START, dependencies); if (bSuccess == false) throw new Exception("Unable to install the service successfully."); } else throw new Exception("The service already exists as an installed service."); }
/// <summary> /// The main entry point for the application. /// </summary> /// <returns>S_OK (0) if no error has occurred, otherwise the error number.</returns> static int Main(string[] args) { var nResult = S_OK; // Setup your default properties ConfigurationFilePath = VirtualServiceStore.VIRTUAL_SERVICE_CONFIG_FOLDER; try { // Declare variables ServiceController scController = null; CustomServiceInstaller cusInstaller = new CustomServiceInstaller(); var bSuccess = false; // Look in the application settings of the configuration file. if (String.IsNullOrEmpty(ConfigurationManager.AppSettings["ServiceName"]) == false) ServiceName = ConfigurationManager.AppSettings["ServiceName"]; else ServiceName = WindowsServiceConstants.SERVICE_NAME; // Parse out the command line parameters ParseCommandLineArguments(args); // Parse out the registry items. ParseRegistry(); // Create your service controller scController = new ServiceController(ServiceName); // Determine how many command line arguments accompanied the executable if (args.Length > 0) { switch (args[0].ToUpper()) { case "-INSTALL": { // Install the service Install(cusInstaller); // Write a success message out Console.WriteLine("The service was installed successfully."); break; } case "-UNINSTALL": case "-REMOVE": { // Make sure that the service is not already uninstalled (never installed) if (cusInstaller.DoesServiceExists(ServiceName) == true) { // Attempt to uninstall the service. bSuccess = cusInstaller.UnInstallService(ServiceName, WindowsServiceConstants.TIMEOUT, WindowsServiceConstants.POLLDELAY); if (bSuccess == false) throw new Exception("Unable to uninstall the service successfully."); // Write out a success message Console.WriteLine("The service was uninstalled successfully."); } else throw new Exception("The service does not exist as an installed service."); break; } case "-STATUS": { // Write the status of the service to the console Console.WriteLine("Service Status:" + scController.Status.ToString() + Environment.NewLine); break; } case "-START": { // Make sure that the service is stopped if (scController.Status == ServiceControllerStatus.Stopped) { // Attempt to start the service scController.Start(); Console.WriteLine("The service was started successfully."); } else Console.WriteLine("Current Service State:" + scController.Status.ToString() + Environment.NewLine + "The service is not in a state which can be started."); break; } case "-STOP": { // Make sure that the service is stopped if (scController.Status == ServiceControllerStatus.Running) { // Attempt to start the service scController.Stop(); Console.WriteLine("The service was stopped successfully."); } else Console.WriteLine("Current Service State:" + scController.Status.ToString() + Environment.NewLine + "The service is not in a state which can be stopped."); break; } case "-PAUSE": { // Make sure that the service is stopped if (scController.Status == ServiceControllerStatus.Running) { // Attempt to start the service scController.Pause(); Console.WriteLine("The service was paused successfully."); } else Console.WriteLine("Current Service State:" + scController.Status.ToString() + Environment.NewLine + "The service is not in a state which can be paused."); break; } case "-CONTINUE": case "-RESUME": { // Make sure that the service is stopped if (scController.Status == ServiceControllerStatus.Paused) { // Attempt to start the service scController.Continue(); Console.WriteLine("The service was resumed successfully."); } else Console.WriteLine("Current Service State:" + scController.Status.ToString() + Environment.NewLine + "The service is not in a state which can be resumed."); break; } case "-CONSOLEHOST": { // Create the console host service controller var svcConsoleHostCtrl = new ServicesManagerConsoleHostContoller(); if (String.IsNullOrEmpty(ConfigurationFilePath) == false) svcConsoleHostCtrl.ConfigurationFolder = ConfigurationFilePath; svcConsoleHostCtrl.Start(); break; } default: { // Output the message telling them about the valid arguments Console.WriteLine(CMDLINE_ARG_MSG); throw new InvalidOperationException("Command line arguments not recognized."); } } } else { // Since no arguments were passed, just attempt to start the service as a normal // windows service (The windows service control manager calls this methods) ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new VSFWinServ(ConfigurationFilePath) }; ServiceBase.Run(ServicesToRun); } } catch (Exception ex) { if (Debugger.IsAttached) Debugger.Break(); Trace.WriteLine(ex); Console.WriteLine("\nUnable to perform the selected action\n\nSource: " + ex.Source + "\nDescription: " + ex.Message); nResult = 69; } return nResult; }