Configure multiple Windows Services in a single project

1

I'm developing a Windows Services project and I've come across the need to run run-time routines and different rules.

Is it possible to have multiple windows services in a single project?

I've tried the following code more unsuccessfully, because this way the exe always calls the instantiated first class:

ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] {
  new ServiceEvento96(),
  new ServiceEvento17()
};

ServiceBase.Run(ServicesToRun);
    
asked by anonymous 28.09.2017 / 19:50

1 answer

0

The solution to this case is very difficult to find, but I think I was able to find it here , I'll explain it to you.

1st - Create a class Installer , it must be public, non-static, inherit from Installer and be decorated with the RunInstaller attribute with true parameter. >

[RunInstaller(true)]
public class MeuServicoInstaller : Installer
{
   public MeuServicoInstaller()
   {
     ServiceProcessInstaller processInstaller = new ServiceProcessInstaller();
     processInstaller.Account = ServiceAccount.LocalSystem;

     ServiceInstaller mainServiceInstaller = new ServiceInstaller();
     mainServiceInstaller.ServiceName = "Service1";
     mainServiceInstaller.Description = "Service One que faz tal coisa";
     mainServiceInstaller.ServicesDependedOn = new string [] { "Service2" };

     ServiceInstaller secondServiceInstaller = new ServiceInstaller();
     secondServiceInstaller.ServiceName = "Service2";
     secondServiceInstaller.Description = "Service Two que faz tal coisa";

     Installers.Add(processInstaller);
     Installers.Add(mainServiceInstaller);
     Installers.Add(secondaryServiceInstaller);
   }
}

In the constructor of this class you add your services in the way that the example shows, attention to the line containing ServicesDependedOn , there it says that serviço 1 depends on serviço 2 , so when the first is called, the second it will also be because it is dependent.

2nd - This class you created will be invoked and instantiated when you run the InstallUtil.exe process.

Note: Note the names of the services, they must be the same. I even advise using the keyword nameof to maintain the consistency of names instead of strings .

Examples:

mainServiceInstaller.ServiceName = nameof(Service1);

mainServiceInstaller.ServicesDependedOn = new string [] { nameof(Service2) };

    
29.09.2017 / 14:41