65.9K
CodeProject is changing. Read more.
Home

Multiple File Watcher Services in One Windows Service Project C#

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.20/5 (2 votes)

Apr 15, 2011

CPOL

1 min read

viewsIcon

18521

Note: There is a Document sub folder and an InstallerBatch sub folder included in the entire solution containing some good information/comments and the installation/uninstallation batch files.

What I am Trying to Achieve

I'm trying to put multiple services into one service, with one same name. So, When you do "net start TheService", it will start Service1() and MySecondUserService().

What I Have Done

I thought adding another service to my project and changing the services to run would be sufficient.
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] {new Service1(), new MySecondUserService()};
ServiceBase.Run(ServicesToRun);

When I install and run this, only the first service is run "Service1".

The solutions is not to add another project installer, but a service installer, which is a component on the project installer. That newly added service installer has to have second service name configured.

Another example?

href="http://stackoverflow.com/questions/2820216/multiple-windows-services-in-a-single-project-mystery">http://stackoverflow.com/questions/2820216/multiple-windows-services-in-a-single-project-mystery

Like code below:

Installer1.cs[Design]

Must have 2 serviceInstaller’s and 1 serviceProcessInstaller

In Installer1.Designer.cs code must be like below:

          #region Component Designer generated code
          /// <summary>
          /// Required method for Designer support - do not modify
          /// the contents of this method with the code editor.
          /// </summary>
          private void InitializeComponent()
          {
              this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
              this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
              this.serviceInstaller3 = new System.ServiceProcess.ServiceInstaller();
              // 
              // serviceProcessInstaller1
              // 
              this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
              this.serviceProcessInstaller1.Password = null;
              this.serviceProcessInstaller1.Username = null;
              // 
              // serviceInstaller1
              // 
              this.serviceInstaller1.Description = "TEST WATCHER1";
              this.serviceInstaller1.DisplayName = "Test Watcher1";
              this.serviceInstaller1.ServiceName = "Test Watcher Service #1";
              // 
              // serviceInstaller3
              // 
              this.serviceInstaller3.Description = "TEST WATCHER3";
              this.serviceInstaller3.DisplayName = "Test Watcher3";
              this.serviceInstaller3.ServiceName = "Test Watcher Service #3";
              // 
              // Installer1
              // 
              this.Installers.AddRange(new System.Configuration.Install.Installer[] {
            this.serviceProcessInstaller1,
            this.serviceInstaller1,
            this.serviceInstaller3});
 
          }
 
          #endregion
 
          private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
          private System.ServiceProcess.ServiceInstaller serviceInstaller1;
          private System.ServiceProcess.ServiceInstaller serviceInstaller3;

In both Service1.Designer.cs and Service3.Designer.cs files, below must be true:

this.ServiceName MUST match this.serviceInstaller1.ServiceName in Installer1.Designer.cs file
 
this.ServiceName MUST match this.serviceInstaller3.ServiceName in Installer1.Designer.cs file

ADVANTAGE

  • Putting multiple services in one single Windows Service .exe file can allow one step installation. One installation will have all the services installed in the Service Controller and all can be viewed in the Services MMC snap-in.
  • Also may be multiple services can share code.

ISSUE

The only issue found so far is that after all the services started, if one or more (but not all) stopped, the "stopped" service(s) is/are still running somehow at the background. To avoid this type of behavior, all other services that still need to run will have to do a restart in order to have all the stopped services truly stop.