Double click to start the Windows service

How to make my windows service work as follows ...

1.) Automatically start after installation

2.) Auto start, even if we just double-click the executable

In other words, I do not want to use the "NET START", "SC" commands and I do not want to run it through the services console. I just want my service to automatically install and automatically start itself ... plus it starts automatically automatically when I double-click the executable file.

Thanks.

+4
source share
4 answers

Take a look at the Topshelf project ( http://topshelf-project.com ) and eliminate the complexity of writing Windows services in .NET. It processes all self-registrations and eliminates all dependencies on the service code from your application.

It is also open source and hosted on GitHub, making it easy to adapt to any application.

(full disclosure, I am one of the authors of the project)

+2
source

See the ServiceController class.

You can use it in commited following:

 [RunInstaller(true)] public class ServiceInstaller : Installer { string serviceName = "MyServiceName"; public ServiceInstaller() { var processInstaller = new ServiceProcessInstaller(); var serviceInstaller = new ServiceInstaller(); processInstaller.Account = ...; processInstaller.Username = ...; processInstaller.Password = ...; serviceInstaller.DisplayName = serviceName; serviceInstaller.StartType = ServiceStartMode.Automatic; serviceInstaller.ServiceName = serviceName; this.Installers.Add(processInstaller); this.Installers.Add(serviceInstaller); this.Committed += new InstallEventHandler(ServiceInstaller_Committed); } void ServiceInstaller_Committed(object sender, InstallEventArgs e) { // Auto Start the Service Once Installation is Finished. var controller = new ServiceController(serviceName); controller.Start(); } } 
+4
source

You can add command line arguments that the installer calls (use ManagedInstallerClass.InstallHelper() ), and code to start the service ...

  public class DataImportService : ServiceBase { // ----------- Other code ----------- static void Main(string[] args) { if (args.Length == 0) { InstallService(false, argValue); break; StartService(); } else { string arg0 = args[0], switchVal = arg0.ToUpper(), argValue = arg0.Contains(":") ? arg0.Substring(arg0.IndexOf(":")) : null; switch (switchVal.Substring(0, 1)) { //Install Service and run case ("I"): case ("-I"): case ("/I"): InstallService(true, argValue); break; // Start Service case ("S"): case ("-S"): case ("/S"): StartService(); default: break; // Install & Start Service case ("IS"): case ("-IS"): case ("/IS"): InstallService(false, argValue); break; StartService(); // Uninstall Service case ("U"): case ("-U"): case ("/U"): InstallService(false, argValue); break; default: break; } } private static void InstallService(bool install, string argFileSpec) { string fileSpec = Assembly.GetExecutingAssembly().Location; if (!String.IsNullOrEmpty(argFileSpec)) fileSpec = argFileSpec; // ------------------------------------------------------------ string[] installerParams = install? new string[] { fileSpec } : new string[] { "/u", fileSpec }; ManagedInstallerClass.InstallHelper(installerParams); } private void StartService() { var ctlr = new ServiceController(); ctlr.ServiceName = "MyService"; // hard code the service name // Start the service ctlr.Start(); } } 
+1
source

My post here shows how to install a Windows service from the command line using the -install option. You can extend this logic to have the -start option, and then create a desktop shortcut that includes this option.

0
source

Source: https://habr.com/ru/post/1314624/


All Articles